[英]I don;t understand what is wrong with this code
Im getting a syntax error for elif option ==2:. 我收到elif选项== 2:的语法错误。 I was wondering what I need to do to fix it.
我想知道如何解决此问题。 I followed the pseudocode my professor gave us but it still won't run.
我遵循了教授给我们的伪代码,但仍然无法运行。 I'm wondering if I shouldn't have used elif or maybe something about the indentation is off.
我想知道我是否应该使用省略号,或者有关缩进的信息已关闭。
import random
print("Welcome to the guess my number program")
while True:
print("1. You guess the number")
print("2. You type a number and see if the computer can guess it")
print("3. Exit")
option = int(input("Please enter your number here: "))
if option ==1:
#generates a random number
mynumber = random.randint(1,11)
#number of guesses
count = 1
while True:
try:
guess = int(input("Guess a number between 1 and 10:"))
while guess < 1 or guess > 10:
guess = int(input("Guess a number between 1 and 10:")) # THIS LINE HERE
except:
print("Numbers Only")
continue
#prints if the number you chose is too low and adds 1 to the counter
if guess < mynumber:
print("The number you chose is too low")
count= count+1
#prints if the number you chose is too high and adds 1 to the counter
elif guess > mynumber:
print("The number you choose is too high")
count = count+1
#If the number you chose is correct it will tell you that you guessed the number and how many attempts it took
elif guess == mynumber:
print("You guessed it in " , count , "attempts")
break
elif option == 2:
number = int(input("Please Enter a Number: "))
count = 1
while True:
randomval = random.randint(1,11)
if (number < randomval):
print("Too high")
elif (number > randomval):
print("Too low")
count = count+1
elif (number==randomval):
print("The computer guessed it in" + count + "attempts. The number was" + randomval)
break
else:
break
The problem is simple. 问题很简单。 There is no continuity between
if option == 1
and elif option == 2
, because of the in between while loop. if option == 1
和elif option == 2
之间没有连续性,因为在while循环之间。 What you have to do is remove the el
part of elif option == 2
and just write if option == 2
. 您要做的是删除
elif option == 2
的el
部分, if option == 2
elif option == 2
写。
I haven't tested the whole program myself. 我还没有亲自测试整个程序。 But at a glance, this should rectify the problem.
乍一看,这应该可以解决问题。
Please comment if otherwise. 否则请发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.