基本上我的程序是一个数字猜谜游戏,包括诸如轮数之类的东西,如果数字超出范围(1和100),将识别该数字是否已经说过。
在创建一个函数(检查它是否是一个整数)之后所以我不在while循环中重复我的代码,我的代码似乎生成了一个新的数字,在每次猜测之后猜测并再次键入相同的数字它会注意并返回说这个号码已经输入过,但之后会说这个号码是否高,我不想要。任何有关如何解决这个问题的帮助都非常有用。
# Higher / Lower version 4
# Basic program works has loop but does not ask for num of rounds or store best score and says if number was already entered.
import random
# List for already said inputs
tried_num = []
# Score
best_score = 25
# Rounds
rounds_played = 0
valid_round = False
# Checks numeric input is valid
def int_check(question,min,max):
valid = False
while not valid:
try:
response = int(input(question))
if response < min or response > max:
print("\n Whoops! Please enter an INTEGER between {} and {}".format(min,max))
else:
valid = True
return(response)
except ValueError:
print("You did not enter an INTEGER. Please try again")
rounds_wanted = int_check("How many rounds would you like to play? (1 - 10): ",1,10)
while rounds_played < rounds_wanted:
# Generate random integer
gen_num = random.randrange(1,100)
print(gen_num)
# Guess
num_guesses = 0
guess = int_check("Guess the number between 1 and 100: ",1,100)
if guess not in tried_num:
num_guesses += 1
tried_num.append(guess)
else:
print("You already guessed that number, Try a differnet one \n")
# Num of guesses till round ends
if num_guesses > 26:
print("You Lose! You have used up your 25 guesses, the number was %s" % gen_num)
elif guess > gen_num:
print("Your guess was to high , Try a lower number \n")
elif guess < gen_num:
print("Your guess was to low , Try a higher number \n")
elif num_guesses == 1:
rounds_played = rounds_played + 1
print("Congratulations you have won, you guessed the number in 1 Guess!!")
else:
rounds_played = rounds_played + 1
print("Congratulations you have won, you guessed the number in %s guesses \n" % num_guesses)
# Update Best Score if number of guesses is lower
if num_guesses < best_score:
best_score = num_guesses
if best_score < 26 :
print("Your best score was: %s!" % best_score)
答案 0 :(得分:0)
虽然你有一个循环来控制所选轮数的播放,但每次围绕该循环的主体你只需要一个猜测。你真的需要在现有循环内部循环来控制单轮的播放。您可以在下面的修订代码中看到嵌套循环结构(使用第三个嵌套循环来处理重复猜测)。
import random
def int_check(question,min,max):
valid = False
while not valid:
try:
response = int(input(question))
if response < min or response > max:
print("\n Whoops! Please enter an INTEGER between {} and {}".format(min,max))
else:
valid = True
return(response)
except ValueError:
print("You did not enter an INTEGER. Please try again")
rounds_wanted = 20
max_guesses = 5
successes = 0
for round_number in range(rounds_wanted):
gen_num = random.randrange(1,100)
print("+++ Game", round_number+1, "- secret is", gen_num)
previous_guesses = []
guess = None
for guess_number in range(max_guesses):
# get the next guess
while not previous_guesses or guess in previous_guesses:
guess = int_check("Guess {}: ".format(guess_number+1), 1, 100)
if guess in previous_guesses:
print("You already guessed", guess, "- please try again")
else:
previous_guesses.append(guess)
break
if guess == gen_num:
successes += 1
print("Well done - you got it!")
break
else:
print("Sorry - try again")
else:
print("I won this one!")
print("Successes so far:", successes, "out of", round_number+1)
print("Thanks for playing")