我需要帮助修复我的代码 - 非常脆弱

时间:2017-01-06 19:25:42

标签: python

我对Python非常陌生,对于我的任务,我必须编写一个简单的'Higher or Lower'风格游戏。

我目前正试图通过输入无效的输入来使用户无法使游戏崩溃,但我仍然停留在要求用户决定支付多少钱的位置他们想要开始。

我尝试过一些事情,但我总是遇到这个错误:

Traceback (most recent call last):
  File "C:/Users/Liam/Desktop/game test 2-6-6.py", line 36, in <module>
    print("\nBank Account: £", LTDbank_account)
NameError: name 'LTDbank_account' is not defined

尽可能多地学习,以便在我的代码中修复此内容或其他任何内容的任何帮助都将非常感谢!

编辑:我还应该提到这是在Python 3.4.3上,如果重要的话

import random

highlow = True

while highlow == True:
    LTDplay = input("Would you like to play Higher or Lower? [ y | n ] ")
    if LTDplay == "y" or LTDplay == "n":
        highlow = False
    else:
        print("invalid entry")

    print("-"*40)                               

while LTDplay == "n":                       
    quit()

LTDscore = 0
LTDtries = 0
LTDwagered = 0              
LTDwon = 0
LTDwin_rate = 0

while LTDplay == "y":

    try:
        LTDbank_account = int(input("Please enter the amount of money you wish to start with: £"))
    except ValueError:
        print("invalid entry")

    LTDdie1 = random.randint(1,10)
    print("\n| Die 1: ", LTDdie1)
    print("\nBank Account: £", LTDbank_account)

    LTDbet = int(input("\nPlease place your bet: £"))
    LTDwagered = LTDwagered + LTDbet    
    LTDwinnings = LTDbet/2              
    LTDguess = input("\nDo you think the next number will be Higher or Lower? [ h | l ] ") 

    print("\nPlease wait while I roll the dice")

    print ("-"*20)

    import time             
    time.sleep(2)

    LTDdie2 = random.randint(1,10)      
    print("\n| Die 2: ", LTDdie2)       

    if LTDguess == "h":                 
        if LTDdie2 > LTDdie1:
            print("\n*** Congratulations You Win! ***")         
            LTDbank_account = LTDbank_account + LTDwinnings
            LTDwon = LTDwon + LTDwinnings
            LTDscore = LTDscore + 1        
            LTDtries = LTDtries + 1
            print("\nScore: ", LTDscore)
            print("-"*40)
        elif LTDdie2 < LTDdie1:
            print("\n*** Sorry You Lose ***")               
            LTDbank_account = LTDbank_account - LTDbet
            LTDtries = LTDtries + 1
            print("\nScore: ", LTDscore)
            print("-"*40)
        elif LTDdie2 == LTDdie1:
            print("\n*** It's a Draw! ***")                 
            LTDtries = LTDtries + 1
            print("\nScore: ", LTDscore)
            print("-"*40)

    if LTDguess == "l":                 
        if LTDdie2 > LTDdie1:
            print("\n*** Sorry You Lose! ***")
            LTDbank_account = LTDbank_account - LTDbet
            LTDtries = LTDtries + 1
            print("\nScore: ", LTDscore)
            print("-"*40)
        elif LTDdie2 < LTDdie1:
            print("\n*** Congratulations You Win! ***")
            LTDscore = LTDscore + 1
            LTDtries = LTDtries + 1
            LTDbank_account = LTDbank_account + LTDwinnings
            LTDwon = LTDwon + LTDwinnings
            print("\nScore: ", LTDscore)
            print("-"*40)
        elif LTDdie2 == LTDdie1:
            print("\n*** It's a Draw! ***") 
            LTDtries = LTDtries + 1
            print("\nScore: ", LTDscore)
            print("-"*40)

    LTDwin_rate = (LTDscore / LTDtries)*100         

    if LTDbank_account <= 0:
        print("\nLooks like you're all out of money!\n\n*** GAME OVER ***")
        print("\n| Tries: ", LTDtries)
        print("\n| Score: ", LTDscore)
        print("\n| Win Rate(%): ", LTDwin_rate)
        print("\n| Wagered: £", LTDwagered)
        print("\n| Winnings: £", LTDwon)

    LTDplay = input("\nDo you want to play again? [ y | n ] ")
    while LTDplay == "n":
        print("-"*40)
        print("\n FINAL STATS")
        print("\n| Tries: ", LTDtries)
        print("\n| Score: ", LTDscore)
        print("\n| Win Rate(%): ", LTDwin_rate)
        print("\n| Wagered: £", LTDwagered)
        print("\n| Winnings: £", LTDwon)

        time.sleep(7)      
        quit()

4 个答案:

答案 0 :(得分:3)

如果第一个条目无效,则打印相应的消息,然后继续游戏而没有任何值。这会导致你看到的问题。

相反,你需要一个循环来获得一个有效的值,更像是

okay = False
while not okay:
    try:
        LTDbank_account = int(input("Please enter ..."))
        okay = True
    except ValueError:
        print("invalid entry")

之后,您满足此循环,您可以继续执行该程序的其余部分。

LTDdie1 = random.randint(1,10)
print("\n| Die 1: ", LTDdie1)
print("\nBank Account: £", LTDbank_account)   # Apparent crash point
...

答案 1 :(得分:1)

在您的代码中,如果用户输入的内容无效,则会打印一条消息,但无论如何都会继续执行代码,LTDbank_account未定义。相反,请尝试循环使用input语句,直到LTDbank_account提供有效值:

while True:
    try:
        LTDbank_account = int(input("Please enter the amount of money you wish to start with: £"))
    except ValueError:
        print("invalid entry")
    else:
        break

答案 2 :(得分:0)

可能发生的事情是你在线上遇到例外:

LTDbank_account = int(input("Please enter the amount of money you wish to start with: £"))

如果这是真的,您会在错误追溯之前看到except块中的文本打印到控制台,特别是:

  

无效输入

您可能需要的是再次提示用户输入金额,直到他提供有效值。您可以在while循环中包装原始的try / except块,并在输入有效时将其从中断:

while True:
    try:
        LTDbank_account = int(input("Please enter the amount of money you wish to start with: £"))
        break
    except:
        print("invalid entry")

答案 3 :(得分:0)

您需要让用户在继续之前输入有效输入。这是一个例子:

try:
    LTDbank_account = int(input("Please enter the amount of money you wish to start with: £"))
except ValueError:
    print("invalid entry")

无论用户输入无效输入,都将继续执行。解决此问题的一种方法是使用开放(无条件)循环。这里的另一点是,如果用户使用£启动条目,它将永远不会转换为整数。我改变了你的提示:

# Input loop. Continue until valid input.
while True:
    try:
        LTDbank_account = int(input("Please enter the amount of money as integer number:"))
        break     # Vaild entry. Break the input loop
    except ValueError:
        print("Seems you didn't enter valid money amount. Please try again.")

我没有检查所有需要修改的地方。仔细检查您的代码并思考会发生什么。然后改变它。