Kir*_*127 -1 python loops python-3.x
我最初在python 2中编写了这个程序,它工作正常,然后我切换到python 3,while循环工作.
我运行程序时没有出现任何错误,但它不会检查运行前或运行期间i的值.无论如何,while循环和第一个if循环都将运行.
#imports the random module
import random
#Creates variable that is used later
i = 0
#chooses a random number betweeen 1 - 100
randomNumber = random.randint(1,10)
#prints the number
print (randomNumber)
#Creates while loop that runs the program until number is guessed
while i == 0:
#Creates a variable where the answer will be stored, and then asked the question in the quotes
user_answer = input("Try to guess the magic number. (1 - 10) ")
print ("\n")
if user_answer == randomNumber:
print("You guessed correct")
break
else:
print("Incorrect. Try again.")
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
您正在比较类似的东西'6' == 6,因为您没有将用户输入转换为int.
替换user_answer = input("Try to guess the magic number. (1 - 10) ")为user_answer = int(input("Try to guess the magic number. (1 - 10) ")).