Python编程,gam语法错误

2024-04-26 04:01:34 发布

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

#makes a varible called name to identify the players name
name=input("Hello person, Whats your name?")
#prints their name
print("Hello", name)
#console asks the player if they want to play the game, if choice is "yes" then continue,   else say "ok bye bye" (at the bottom!!)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
    print("Ok", name,", listen up")    
    print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
    housename=input("What do you want to call the house?")
    print("The old,",housename,"was once owned by an old lady. You decide to go up to   the", housename, ",you encounter a ghost in your path. You see a varitety of weapons beside  you, an axe, sword and a bow.")
#asks the player if they want an axe sword or bow    
    choice3=input("Do you choose the axe, sword or bow?")
#if the choice is "bow" then proceed with this code    
    if choice3==("bow" or "Bow" or "bow " or "Bow "):
        print("You equip the shoddy Bow, The bow feels as if it could snap any second.")
#sets the enemyshealth as 10
        enemyhealth=int(10)
#makes a while loop to keep the battle going instead of 1 time.
        while enemyhealth >= 1:
            print("Take a shot!")
            bowattack=input("Type attack to fire an arrow!")
            if bowattack==("attack"):
                import random
#randomiser for damage generator
                damage = ["1", "2", "3", "4"]
                damage2 = int(random.choice(damage))
                enemyhealth = enemyhealth - damage2
                print("The ghost took some damage. Enemys health:", enemyhealth)

            else:
                print("Are you sure you typed shoot?")
#if the enemys health gets below 1 print you killed the ghost, reward system! **this is what im having trouble with!!**
    if enemyhealth <= 1:
        print("You killed the Ghost!!")
        print("You vanquished the ghost, you now collect a new weapon!")
#confirms the reward, either gives the player a shiny bow or a double shot bow.
        import random
        reward = ["Shiny bow", "Doubleshot bow"]
#randomiser for either reward
        reward2 =(random.choice(reward)
#prints what weapon the player got
#THIS IS THE PROBLEM, ON THIS LINE
        print("You got a:", reward2)



    #pointless easteregg :D        
    elif choice==("maybe"):
        print("You found an easter egg, congrats. PS this does nothing")
#if the player typed anything other than yes say ok bye bye.
else:
    print("Ok, bye bye", name)

我完全知道代码中还没有其他两个if语句。我遇到的麻烦是杀死鬼魂的奖励发生器。我认为这是一个缩进错误;它说的是打印行的语法错误。你知道吗

我知道这是相当多的代码,但我真的很感激,如果能帮我解决这个问题,如果你看到什么,我可以做一个捷径,这也将是有益的。 我正在使用python3.4.2!你知道吗


Tags: orthetonameyouaninputif
2条回答

而且,行if choice==('yes' or 'yes ' or 'Yes' or 'Yes '):也不起作用。你知道吗

试试这个:

if choice.strip().lower() == 'yes':
    # whatever

strip将删除空白。lower将字符串放在小写形式

同样的情况也发生在choice3

前一行中缺少右括号:

reward2 =(random.choice(reward)

应该是:

reward2 =(random.choice(reward))

相关问题 更多 >