【问题标题】:How to ask someone if they want to play again如何询问某人是否想再玩一次
【发布时间】:2016-06-02 00:45:21
【问题描述】:

我不知道如何询问用户是否想再玩一次,如果他们说y 它会循环回到开始或n 并退出游戏,我不知道该怎么做。我知道我可以使用循环,但我不知道如何使用它们。

import random

xp = 0

level = 1

player1 = raw_input("player1 enter name")

enemyhealth = 100

playerhealth = 100

stage2 = "false"

difficulty = raw_input("choose difficulty, rookie,pro,master,legend or god")

if difficulty == "rookie":

    enemyhealth = enemyhealth - 15

if difficulty == "pro":

    enemyhealth = enemyhealth + 10

if difficulty == "master":

    enemyhealth = enemyhealth + 35

if difficulty == "legend":

    enemyhealth = enemyhealth + 40

if difficulty == "god":

    enemyhealth = enemyhealth + 60                                                            

print ("A quick tutorial: Make sure you type you actions with no spaces,  heres a list of the moves you can perform")

print ("Punch: A simple attack that has a short range of damage but is reliable.")

print ("flyingkick: not 100 percent reliable but if pulled off correctly can deal good damage.")

print ("uppercut: A somewhat reliable move that deals decent damage.")

print ("kick: A simple attack that has a short range of damage but is reliable.")

print ("stab: can be very powerful or deal next to no damage.")

print ("shoot: deadly if it hits, less so if you miss.")

print ("A man in the street starts a fight with you, beat him!")




while enemyhealth >0:

    fight = raw_input("fight")

    if fight == "punch":

        print ("You punched the enemy")

        enemyhealth = enemyhealth - random.randint(5,10)

        xp = xp + 1

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (0,5)

        print "you now have..", playerhealth

    elif fight =="flyingkick":

        print "you flying kicked the enemy"

        enemyhealth = enemyhealth - random.randint(5,25)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,15)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (0,5)

        print "you now have..", playerhealth

    elif fight =="uppercut":

        print "you uppercutted the enemy"

        enemyhealth = enemyhealth - random.randint(10,25)                                      

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,15)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="rko":

        print "you rko'ed the enemy, out of nowhere!"

        enemyhealth = enemyhealth - random.randint(9,29)                                      

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,12)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="kick":

        print "you kicked the enemy"

        enemyhealth = enemyhealth - random.randint(5,10)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="ninjastar":

        print "you ninjastarred the enemy"

        enemyhealth = enemyhealth - random.randint(5,20)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="headbutt":

        print "you headbutted the enemy"

        enemyhealth = enemyhealth - random.randint(5,18)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="deathray":

        print "you deathrayed the enemy"

        enemyhealth = enemyhealth - random.randint(1,50)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(1,30)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="stab":

        print "you stabbed the enemy"

        enemyhealth = enemyhealth - random.randint(3,40)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(2,20)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="shoot":

        print "you shot the enemy"

        enemyhealth = enemyhealth - random.randint(1,50)

        xp = xp + 1 

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(1,30)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    if xp == 5:

       print "you leveled up! move unlocked: 'rko'"

    if xp == 7:
       print "you leveled up! move unlocked: 'ninjastar'"

    if xp == 10:
       print "you leveled up! move unlocked: 'headbutt'"

    if xp == 100:
       print " you reached the max level! move 'deathray' is now unlocked"

    if playerhealth <1:

        print "you died!"

    if enemyhealth < 1:

        print "the enemy is KO , YOU WIN , Stage 2 is unlocked and the enemy has become stronger!"

        stage2 = "true"

    else:

        None

【问题讨论】:

  • “请不要说将代码包装在一个 while 循环中,因为我不知道这意味着什么,哈哈,谢谢,请不要投反对票” - 很遗憾。那么,您为什么不了解while 循环,然后像成年人一样写作?
  • 请在你的编辑器中增加行高,这样你就不用这样写了。
  • @alexbwithtek 我们应该关心您的截止日期,因为...?请收下tour,学习How to Ask
  • @alex bwithtek 我知道你是一个新程序员,特别是 python,所以我试图激发....

标签: python python-2.7


【解决方案1】:

我建议你的是: 阅读 python 文档和函数式编程。之后,您可以使用此方法来获得理想的结果...

你可以从中得到启发:

#!/usr/bin/env python
import sys
import random

def play_game():
    print ' I am playing...' #Put your code here

def repeat(answer):
    if answer == 'y':
        print ' Good...'
        play_game()
    elif answer == 'n':
        print ' Exitting...'
        sys.exit()
    else:
        print ' Invalid choice!'
        return 'continue'

if __name__ == '__main__':
    try:
        play_game()
        while True:
            answer = raw_input(' Do you want to play again? [y/n]')
            ans = repeat(answer)
            if ans == 'continue':
                continue
    except KeyboardInterrupt:
        print '\n Ctrl+c signal recieved!'
        sys.exit()

【讨论】:

  • 这会在你第一次玩之前询问你是否想再玩一次。
  • 我建议您定义repeat() 以提示您提出论点并让 提出问题。然后,您可以在您的repeat() 函数中放置一个while 循环,如果答案无效,它将询问[y/n]。它应该一直询问直到它得到一个有效的答案,然后返回一个表示响应的布尔值。那么,在你的try 块中,你可以说while True: play_game() if not repeat("Do you want to play again? [y/n]"): sys.exit()。但这只是一个建议。
  • 没问题...这也是可能的 我试过这样尝试...谢谢///
猜你喜欢
  • 2020-02-15
  • 2016-09-22
  • 2012-02-19
  • 2017-08-22
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 2017-01-30
  • 2012-10-11
相关资源
最近更新 更多