这是我的代码;
running = True
while running:
print("Open Supply Drop?.")
answer = input()
if answer == ("Yes"):
print("Please wait 3 seconds...")
import time
time.sleep(3)
import random
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility', 'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip', 'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard)
elif answer == ("yes"):
print("Please wait 3 seconds...")
import time
time.sleep(3)
import random
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility', 'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip', 'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard))
else:
print("Please wait 3 seconds...")
import time`enter code here`
time.sleep(3)
如果,elif和else有语法错误?我只改变了一些东西,并且工作得非常好,但现在无论如何它都会这样,这是否与缩进有关?
答案 0 :(得分:1)
if answer == ("Yes"):
print("Please wait 3 seconds...")
import time
time.sleep(3)
import random
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility', 'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip', 'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard) # <--You're missing one closing parenthesis here. Try placing the parenthesis to fix the syntax error.
这是您的代码的临时版本: 我也猜测IF,ELSE应该在while循环中缩进?
import time
import random
while True:
print("Open Supply Drop?.")
answer = input("")
if answer.upper() == "YES": #.upper() converts the the full string to CAPITAL letters, this allows you to remove all the unnecessary code under elif
print("Please wait 3 seconds...")
time.sleep(3)
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility',
'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip',
'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard))
else:
print("Please wait 3 seconds...")
time.sleep(3)}
break
答案 1 :(得分:0)
您的缩进不一致。 Python完全基于缩进语法,你需要在IF,ELIF和ELSE标签下的所有行都有相同的数量。 此外,你将有一个无限循环,因为你设置running = True但从未将其设置为其他任何东西。希望有所帮助。
答案 2 :(得分:0)
正如已经提到的,你在一条线路上错过了一个人。你有几个问题,所以我写了一个清理版本
callingcard
列表被拉出循环。它被重写为一个不可变的元组(列表意味着你计划修改)和
添加换行符以保持行&lt; 80个字符break
代替while
Yes
和yes
示例
import time
import random
callingcard = ('BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility',
'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip',
'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol')
while True:
print("Open Supply Drop?.")
answer = input().lower()
if answer == "yes":
print("Please wait 3 seconds...")
time.sleep(3)
print("You got...")
print(random.choice(callingcard))
else:
print("Please wait 3 seconds...")
time.sleep(3)
break
答案 3 :(得分:0)
这很有效。解释如下。
import random
import time
running=True
print("Open Supply Drop?.")
while running:
answer = input()
if answer == "Yes":
print("Please wait 3 seconds...")
time.sleep(3)
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility', 'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip', 'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard))
elif answer == "yes":
print("Please wait 3 seconds...")
time.sleep(3)
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility', 'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip', 'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard))
else:
running=False
print("Please wait 3 seconds...")
time.sleep(3)
1)为什么import
在每个if/elif/else
语句中import
位于顶部一次。那很好。
2)语法错误似乎也是由于混合制表符和缩进的空格。空格是缩进的首选方法。基本上我改变并检查你的每个缩进,以摆脱语法错误。
3)更改running=False
的值,如果是/是,则不是answer
但是你真的不需要另一个elif。因为这与你的if完全相同。 您可以改为:
import random
import time
running=True
print("Open Supply Drop?.")
while running:
answer = input()
if answer.lower() == "yes":
print("Please wait 3 seconds...")
time.sleep(3)
callingcard = ['BootsOnTheGround', 'TitanBaseCamp', 'TitanFacility', 'TitanicStorm', 'TitanicCanyon', 'TitanSandstorm', 'ToTheShip', 'AsteroidMines', 'TheSteelDragon', 'C6Assembly', 'C12Patrol']
print("You got...")
print(random.choice(callingcard))
else:
running=False
print("Please wait 3 seconds...")
time.sleep(3)
print('Exit')
使用lowercase
将所有字符转换为lower()
并仅使用yes
进行检查即可。
输出:
Open Supply Drop?.
Yes
Please wait 3 seconds...
You got...
C12Patrol
YES
Please wait 3 seconds...
You got...
TitanSandstorm
yes
Please wait 3 seconds...
You got...
TitanicCanyon
yEs
Please wait 3 seconds...
You got...
AsteroidMines
no
Please wait 3 seconds...
Exit