使用while循环无法获得正确的计数值
尝试使用全局变量,但无法理解其用法甚至语法
尝试使用变量外部的计数
print('I am thinking of a number between 1 and 20')
selectedNumber=12
while True:
user_number=int(input("Please enter your number?\n"))
if user_number<selectedNumber:
print('Your guess is too low')
global count=count+1
elif user_number==selectedNumber:
break
else:
print('Your guess is too high')
print('Great Job', count)
答案 0 :(得分:0)
我建议在循环外部定义count
变量,然后在每次循环运行时(而不是在条件内部)递增该变量;也许像这样:
print('I am thinking of a number between 1 and 20')
selected_number = 12
count = 0
while True:
user_number = int(input("Please enter your number: "))
count += 1
if user_number < selected_number:
print('Your guess is too low')
elif user_number == selectedNumber:
break
else:
print('Your guess is too high')
print('Great Job', count)