我认为如果我只是逐字地解答您的问题,这是最简单的:
“编写一个python程序,不断向用户询问10以外的数字。如果输入10,则谴责用户他们不要猜数字10”。
我收到错误 - 基本上,我不知道在进入while循环之前如何定义guess变量。
target, guess = 10
while target != guess:
guess = int(input('Enter a number other than 10: '))
print("Hey, you weren't supposed to enter 10!")
答案 0 :(得分:2)
您可以将元组分配给这样的元组
target, guess = 10, 10
你实际上并不希望它们是相同的,因为while循环立即退出。
你可以说
target, guess = 10, None
但通常用两行来写这个更清楚
target = 10
guess = None
另一种方法是避免在第一时间分配一个虚拟值来猜测
target = 10
while True:
guess = int(input('Enter a number other than 10: '))
if guess == target:
break
print('Hey, you werent supposed to enter 10!')
答案 1 :(得分:0)
创建该错误是因为您尝试将两个变量分配给一个事物(整数10
)。如果您希望两个变量都等于10
,则需要执行以下操作:
target = guess = 10
但是,在此程序中,您不需要该功能。相反,你会想要这样的东西:
# Set target equal to 10
target = 10
# Make guess equal None, since there are no guesses yet
guess = None
# Loop until guess equals target
while target != guess:
# Get new guess
guess = int(input('Enter a number other than 10: '))
# Print message
print('Hey, you werent supposed to enter 10!')
答案 2 :(得分:0)
只需创建一个变量" num"价值不是10(我已经拿了11) 然后创建一个局部变量作为num并输入你喜欢的值,你将获得结果
num=11
while num!=10:
num=int(input("Enter a number other than 10"))
print "I thought i said no to 10"
答案 3 :(得分:0)
你可以这样做......
condition = True
while condition == True:
number = int(input("Enter a number"))
if number == 10:
print("You can't enter ten") #or whatever you want to say
else:
condition = False #if you want to drop out of the loop
希望这有助于:)