Python,if-else语句错误
无论我输入什么作为变量“choice”,带有错误标记的代码行都不会运行。有什么帮助吗 我也欢迎我能做的任何事情来帮助清理我的代码,非常新:p -我在该网站上弄乱了格式的代码如果您的选择==dorD:later simplify cases行有错误的标识。取消该行:Python,if-else语句错误,python,if-statement,Python,If Statement,无论我输入什么作为变量“choice”,带有错误标记的代码行都不会运行。有什么帮助吗 我也欢迎我能做的任何事情来帮助清理我的代码,非常新:p -我在该网站上弄乱了格式的代码如果您的选择==dorD:later simplify cases行有错误的标识。取消该行: import time import random print("Welcome to double up!") pscore = 1 pscore = str(pscore) print("Your current balance
import time
import random
print("Welcome to double up!")
pscore = 1
pscore = str(pscore)
print("Your current balance is "+pscore+", would you like to double or
quit?")
while pscore != 0:
pscore = str(pscore)
print("Your current balance is "+pscore+"!")
pscore = int(pscore)
choice = input("d/q :")
if choice == ("d")or("D"): #later simplify cases
luck = random.randint(1,100)
if luck > 75:
print("Upgrade failed!")
pscore = 0
else:
print("Upgrade complete!")
pscore = pscore * 2
else: #ERROR
print("Incorrect command! Please retry!")
两个问题:一个是线路问题
if choice == ("d")or("D"): #later simplify cases
必须是无牵连的。第二个问题是if语句返回true if choice==d或if d,其中第二个始终为true。取消输入if选项。。。一行一行。然后,将if语句更改为:
if choice == ("d")or("D"): #later simplify cases
# import time - Unused. you can remove this line
import random
print("Welcome to double up!")
pscore = 1
pscore = str(pscore)
print("Your current balance is "+pscore+", would you like to double or quit?")
while pscore != 0:
pscore = str(pscore)
print("Your current balance is "+pscore+"!")
pscore = int(pscore)
choice = input("d/q :")
if choice == ("d")or("D"): #later simplify cases # <<< Indent here.
luck = random.randint(1,100)
if luck > 75:
print("Upgrade failed!")
pscore = 0
else:
print("Upgrade complete!")
pscore = pscore * 2
else: #ERROR
print("Incorrect command! Please retry!")
您以前所做的将始终计算为true,而else将永远不会执行。它绝对不应该运行。您有缩进错误。@cᴏʟᴅsᴘᴇᴇᴅ 好的,你能告诉我在哪里吗?如果choice==dorD:稍后简化案例,不包括这一个level@AyyLmao一旦缩进问题得到解决,这可能是一个重复的不知道为什么你被否决,这是正确的。缩进是一个问题,但它不是唯一的问题。缩进问题甚至可能不存在于OP的原始代码中,否则它将完全无法运行,而不是执行但从不执行else。缩进是一个问题,但它不是唯一的问题。缩进问题甚至可能不存在于OP的原始代码中,否则它将完全无法运行,而不是执行但从不执行else。
if choice in ('d', 'D'):