显然这是一个重复的问题,但我在发布之前进行了搜索,并且找不到任何有用的东西。我不知道多次问过它。
首先让我说我这是一个菜鸟,所以我的行话不是很划清,所以如果你觉得我没有正确描述我的问题,请保持温柔。谢谢。
我已经编写了这个脚本并且完美无缺:
import random
secret = random.randint(1,99)
guess = 0
tries = 0
print ("You have six tries to guess the secret number between 1 and 99")
guess = int(input("What's first your guess?"))
while tries < 5 and guess != secret:
if guess < secret:
print ("Too low.")
tries = tries + 1
if tries == 1:
guess = int(input("What's your second guess?"))
if tries == 2:
guess = int(input("What's your third guess?"))
if tries == 3:
guess = int(input("What's your fourth guess?"))
if tries == 4:
guess = int(input("What's your fifth guess?"))
if tries == 5:
guess = int(input("What's your sixth guess?"))
elif guess > secret:
print ("too high")
tries = tries + 1
if tries == 1:
guess = int(input("What's your second guess?"))
if tries == 2:
guess = int(input("What's your third guess?"))
if tries == 3:
guess = int(input("What's your fourth guess?"))
if tries == 4:
guess = int(input("What's your fifth guess?"))
if tries == 5:
guess = int(input("What's your sixth guess?"))
if guess == secret:
print ("you got it!")
else:
print ('No more guesses. The secret number was', secret)
............................................... ................................. 但是,当我按如下方式定义函数时:
def ordinal_guess():
if tries == 1:
guess = int(input("What's your second guess?"))
if tries == 2:
guess = int(input("What's your third guess?"))
if tries == 3:
guess = int(input("What's your fourth guess?"))
if tries == 4:
guess = int(input("What's your fifth guess?"))
if tries == 5:
guess = int(input("What's your sixth guess?"))
并按如下方式调用该函数:
while tries < 5 and guess != secret:
if guess < secret:
print ("Too low.")
tries = tries + 1
ordinal_guess()
elif guess > secret:
print ("too high")
tries = tries + 1
ordinal_guess()
脚本不再想玩得好,告诉我输入的猜测总是太低或太高。见下文:
您有六次尝试猜测1到99之间的密码 你的第一个猜测是什么?99 太高 你的第二个猜测是什么?1 太高 你的第三个猜测是什么?3 太高 你的第四个猜测是什么?5 太高 你的第五个猜测是什么?85 太高 你的第六个猜测是什么?50 没有更多的猜测。秘密号码是81
显然,其中一些数字并不高。同样在这里: 您有六次尝试猜测1到99之间的密码 你的第一个猜测是什么?50 太低。 你的第二次猜测是什么?99 太低。 你的第三个猜测是什么?80 太低。 你的第四个猜测是什么?70 太低。 你的第五个猜测是什么?65 太低。 你的第六个猜测是什么?53 没有更多的猜测。秘密号码是75
我做错了什么?