为什么edit()会导致回溯?

2024-09-26 22:52:05 发布

您现在位置:Python中文网/ 问答频道 /正文

非常感谢你们的帮助,我在10年级计算机编程,是的,这是我的最终评估。我按照你的建议修改了代码。。但我想补充的最后一点是,似乎在其他任何地方都找不到。在选项1的while循环之后,如果输入是“N”,那么我希望程序结束。但是,当它这样做时,输出是

Okay, have a good day!

Traceback (most recent call last):
  File "/Users/Zac_R/Documents/School/Grade 10/Computer Science/H1Z1 Text  
    Adventure.py", line 224, in 0
builtins.SystemExit:

有没有办法去掉这个,让它只说“好的,祝你愉快!”?你知道吗

最后,当我说“请输入有效命令!”,类型光标移到下一行(**),而不是停留在我想要的位置(*)。例如:

请输入有效命令![是/否]:* **你知道吗

参考代码如下:

import time
import random
from sys import exit


def play ():

    # Health variables
    userHealth = 20
    zombieHealth = 15

    # This is the actual game
    print ( "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}" )
    print ( "{------------=[Entering The Apocalypse!]=------------}" )
    print ( "{----------------------=[H1Z1]=----------------------}" )
    print ( "{---------------=[Made by: Zac Roter]=---------------}" )
    print ( "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}" ) 
    time.sleep (1)

    print ( "You wake up in a small hospital room. You do not remember how you got here..." )
    time.sleep (1)

    print ( "It is dark and you can only make out a stick on the ground." )
    time.sleep (1)

    choice2 = input( "Do you take it? [Y/N]: " ) 

    # Stick taken
    if (choice2 == "Y"):
        print ( "Stick obtained!" )
        stick = 1


    # Stick not taken
    else:
        print ( "You did not take the stick" )
        stick = 0


    print ( "As you walk towards the only exit, you hear a faint voice ahead. " )
    time.sleep (1)

    choice3 = input( "Do you approach the voice? [Y/N]: " ) 

    # Approach zombie
    if (choice3 == "Y"):
        print ( "You approach the sound..." )
        time.sleep (1)
        print ( "As you get closer, you begin to make out the source of the sound was from a zombie!")
        choice4 = input( "Do you try to fight it? [Y/N]: ") 

        # Fight zombie
        if (choice4 == "Y"):

            # With stick
            if (stick == 1):
                print ( "You only have a stick to fight with!" )
                time.sleep (1)
                print ( "You swiftly strike the zombie in the leg to slow it down." )
                time.sleep (1)
                print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
                print ("                    Loading...                  ")
                print ("         STICK DOES 2-8 DAMAGE PER TURN         ")
                print ("        ZOMBIE DOES 1-6 DAMAGE PER TURN         ")
                print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
                time.sleep(1)    
                while (zombieHealth <= 0 and userHealth <= 0):
                    zombieHealth = zombieHealth - int(random.randint(2,9) )
                    userHealth = userHealth - int(random.randint(1,7) )
                    print ("Zombie has...", zombieHealth, "health left!" )
                    print ("You have...", userHealth, "health left!" )
                    time.sleep (1)

                if (zombieHealth <= 0 and userHealth > 0):
                    print ( "You have killed the zombie!" )

                if (userHealth <= 0 and zombieHealth > 0):
                    print ( "The zombie has killed you!" )
                    playagain()

                if (userHealth <= 0 and zombieHealth <= 0):
                    print ( "You kill the zombie, but you die of the zombie bite." )
                    playagain()

            # Without stick
            else:
                print ( "You don't have anything to fight with!" )
                time.sleep(1)
                print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
                print ("                    Loading...                  ")
                print ("          HANDS DO 1-6 DAMAGE PER TURN          ")
                print ("        ZOMBIE DOES 1-6 DAMAGE PER TURN         ")
                print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
                time.sleep(1)    
                while (zombieHealth > 0 and userHealth > 0):
                    zombieHealth = zombieHealth - int(random.randint(1,7) )
                    userHealth = userHealth - int(random.randint(1,7) )
                    print ("Zombie has...", zombieHealth, "health left!" )
                    print ("You have...", userHealth, "health left!" )
                    time.sleep (1)

                    if (zombieHealth <= 0 and userHealth > 0):
                        print ( "You have killed the zombie!" )

                    elif (userHealth <= 0 and zombieHealth > 0):
                        print ( "The zombie has killed you!" )
                        playagain()

                    else:
                        print ( "You kill the zombie, but you die of the zombie bite." )
                        playagain()






        #Don't fight zombie         
        else:            
            print ( "You choose not to fight the zombie." )
            time.sleep(1)
            print ( "As you turn away, it sprints and kills you!!!" )
            playagain()



    # Don't approach zombie
    else:
        print ( "As you turn away, a zombie sprints and kills you!!!" )
        playagain()


    print ( "Finally, you reach the exit to open doors to an unidentifiable world." )
    time.sleep (1)

    print ( "After a small victory, you hear the sounds of more zombies coming towards you from all angles." )
    time.sleep (1)

    print ( "Suddenly an SUV drives over the zombies in front of you!" )
    time.sleep (1)

    print ( "[Driver]: Get in, dammit!" )
    time.sleep (1)

    choice5 = input( "Do you get in the SUV? [Y/N]: " )
    time.sleep (1)

    # Enter SUV

    if (choice5 == "Y"):
        print ( "As you enter the SUV, the driver injects you with a syringe." )
        playagain()


    else: 
        print ( "You decline his offer, and as the SUV drives away, the zombies devour your remains." )
        playagain()


# The play again option that occurs after every user death
def playagain ():

    print ( "Thank you for playing H1Z1, by Zac Roter." )
    choice6 = input( "Would you like to play again? [Y/N]: " ) 

    if (choice6 == "Y"):
        play()

    if (choice6 == "N"):
        print ( "Okay, have a good day!" )
        exit()

    while (choice6 != "Y" or "N"):
        print ( "Please enter a valid command! [Y/N]: " )
        choice6 = input()

        if (choice1 == "Y"):
            print ( "Starting game..." )
            play()

        elif (choice1 == "N"):
            print ( "Okay, have a good day!" )
            exit()

# The main menu of the program
print ( "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}" )
print ( "{-------------------=[Welcome To]=-------------------}" )
print ( "{----------------------=[H1Z1]=----------------------}" )
print ( "{---------------=[Made by: Zac Roter]=---------------}" )
print ( "{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}" )
time.sleep (1)


name = input( "Greetings survivor, what is your name?: " )
time.sleep (1)

print ( "Welcome "+name+", to H1Z1!" )
time.sleep (1)

print ( "The objective of this game is to survive in a zombie apocalypse." )
time.sleep (1)

choice1 = input( "Would you like to play? [Y/N]: " )

if (choice1 == "Y"):
    print ( "Starting game..." )
    play()

if (choice1 == "N"):
    print ( "Okay, have a good day!" )
    exit()


while (choice1 != "Y" or "N" ):
    print ( "Please enter a valid command! [Y/N]: " )
    choice1 = input()

    if (choice1 == "Y"):
        print ( "Starting game..." )
        play()

    elif (choice1 == "N"):
        print ( "Okay, have a good day!" )
        exit()

你们太棒了!你知道吗


Tags: andthetoyouinputiftimehave
2条回答

选项1的值不会在while循环中更新,因此选项1将保留“Y”、“Y”、“N”或“N”以外的值。您需要再次从用户获取输入,以便更新此变量。比如:

while(choice1 != "Y" or "y" or "N" or "n"):
    print("Please enter a valid command!")
    choice1 = input()

对此进行分析:

>>> def a():
...    print("Type something: ")
...    a = input()
...
>>> a()
Type something: 
La la la la la la # That's my input

python3中的printing是一个函数,有多个隐藏参数,您很少(如果有的话)输入这些参数。如果您在解释器中键入help(print),它可以很好地解释:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep:  string inserted between values, default a space.
end:  string appended after the last value, default a newline.

这里的重点在于这一部分:end = '\n'。在函数内部,实际情况如下:

print my string, and add the "end" value. Flush to the screen (writing to the screen)

您有两种选择:

调用print时更改end关键字参数:

print("Enter a valid command!", end = "")
choice6 = input()

或者删除print调用。将字符串放入input中。你知道吗

choice6 = input("Enter a valid command! ")

这将导致:

Enter a valid command! Never! # "Never!" is my input

相关问题 更多 >

    热门问题