隨機取一值:
random.randint(1, 100) -->0~100之間隨機取一個數字
範圍:
range(1, 6) --> 起始值1 至 終止值 6, 因此會跑 1,2,3,4,5 這個range.
範圍延伸:
range(1, 6, 2) --> 起始值1 至 終止值 6, 中間區隔2, 因此跑 1, 3, 5 這個range.
輸出:
x = sample
y = 123
print('This is output : {}{}'.format(x, y))
結果會顯示:
This is output : sample123
# cyw 20220530, number guessing game
import random
secretNumber = random.randint(1, 100)
print('I\'m thinking of a number between 1 to 100.')
low = 0
high = 100
print(secretNumber)
# Ask the player to input a number.
for guessesTaken in range(1, 6):
print('You have 5 chance to guess. This is the {} times.'.format(guessesTaken))
guess = int(input())
if guess < secretNumber:
print('Try again, your guess is too low.')
low = guess
print('Now {} to {}.'.format(low, high))
elif guess > secretNumber:
print('Try again, your guess is too high.')
high = guess
print('Now {} to {}.'.format(low, high))
else:
break # if player guess the correct number.
if guess == secretNumber:
print('You guess the correct number {}'.format(guess))
else:
print('Too bad, the secretNumber is {}, the last number you guess is {}.'.format(
secretNumber, guess))
沒有留言:
張貼留言
問題沒有大小或好壞