Python,if-else语句错误

时间:2017-07-21 18:18:21

标签: python if-statement

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!")

标记为#ERROR的代码行无法运行,无论我输入什么作为变量'choice'任何帮助?

我也欢迎任何我可以做的事情来帮助清理我的代码,非常新:p

https://pastebin.com/dPbpZfcy - 我在本网站上搞乱格式的代码

5 个答案:

答案 0 :(得分:1)

if choice ...行取消一级。然后,将if语句更改为:

if choice in ('d', 'D'):

您之前所拥有的内容将始终评估为true,else将永远不会被执行。

答案 1 :(得分:0)

您的if choice == ("d")or("D"): #later simplify cases行标识错误。

答案 2 :(得分:0)

Unindent这一行:

if choice == ("d")or("D"): #later simplify cases

答案 3 :(得分:0)

两个问题:一个是行

if choice == ("d")or("D"): #later simplify cases

必须是缩进的。第二个问题是if语句如果choice == ("d") 则返回true,如果("D")则返回,第二个问题始终为真。

答案 4 :(得分:0)

# 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!")