虽然这个功能还在执行之后吗?

时间:2014-04-29 00:54:28

标签: python python-3.x

我做了一个简单的选择游戏,就像使用Python的Rock,Paper,Scissors一样。问题是,在你获胜并输入胜利者的名字之后,while循环仍然会再次执行。这是无法接受的!我看了看它,然后又看了一遍。据我所知,我似乎无法解决问题。这是一个很好的学习时刻。

链接到File

# Imports modules
import random
import math

# Welcome message
def main():

    print("//////////////////////////////////////")
    print("//////////////////////////////////////")
    print("// Welcome Ninja, Pirate, or Zombie //")
    print("//////////////////////////////////////")
    print("//////////////////////////////////////")
    print("\n\n")

    choose()

# Prompts user to choose class
def choose():
    choice = str(input("Make a choice! (Ninja, Pirate, or Zombie) "))

    choice = choice.lower()

    if choice == "ninja" or choice == "pirate" or choice == "zombie":
        enemy(choice)
    else:
        choose()

# Randomly selects opposing enemy
def enemy(choice):

    enemyRandom = random.randint(1,3)

    if enemyRandom == 1:
        enemy = "ninja"
    elif enemyRandom == 2:
        enemy = "pirate"
    elif enemyRandom == 3:
        enemy = "zombie"
    else:
        print("Something went wrong!")

    hit_factor(choice, enemy)

# Determines the hit factor. Certain class are weak or strong when fighting certain
# other classes
def hit_factor(choice, enemy):

    if choice == "ninja" and enemy == "ninja":
        hitFactor = 1
    elif choice == "ninja" and enemy == "pirate":
        hitFactor = 1.2
    elif choice == "ninja" and enemy == "zombie":
        hitFactor = 0.8
    elif choice == "pirate" and enemy == "ninja":
        hitFactor = 0.8
    elif choice == "pirate" and  enemy == "pirate":
        hitFactor = 1
    elif choice == "pirate" and enemy == "zombie":
        hitFactor = 1.2
    elif choice == "zombie" and enemy == "ninja":
        hitFactor = 1.2
    elif choice == "zombie" and enemy == "pirate":
        hitFactor = 0.8
    elif choice == "zombie" and enemy == "zombie":
        hitFactor = 1
    else:
        print("Something went horribly wrong.")

    enemy_hit_factor(choice, enemy, hitFactor)

# Determines the enemy's hit factor
def enemy_hit_factor(choice, enemy, hitFactor):

    if enemy == "ninja" and choice == "ninja":
        enemyHitFactor = 1
    elif enemy == "ninja" and choice == "pirate":
        enemyHitFactor = 1.2
    elif enemy == "ninja" and choice == "zombie":
        enemyHitFactor = 0.8
    elif enemy == "pirate" and choice == "ninja":
        enemyHitFactor = 0.8
    elif enemy == "pirate" and choice == "pirate":
        enemyHitFactor = 1
    elif enemy == "pirate" and choice == "zombie":
        enemyHitFactor = 1.2
    elif enemy == "zombie" and choice == "ninja":
        enemyHitFactor = 1.2
    elif enemy == "zombie" and choice == "pirate":
        enemyHitFactor = 0.8
    elif enemy == "zombie" and choice == "zombie":
        enemyHitFactor = 1
    else:
        print("Something went horribly wrong.")

    combat(choice, enemy, hitFactor, enemyHitFactor)

# Initiates combat
def combat(choice, enemy, hitFactor, enemyHitFactor):

    yourHP = 1000
    enemyHP = 1000

    print("Your HP: ", yourHP)
    print("Enemy's HP: ", enemyHP)

    over = False

    while over != True:
        isHitCalc = random.randint(1,10)
        if isHitCalc > 3:
            isHit = True
        else:
            isHit = False
            print("You missed!")

        if isHit == True:
            randomHIT = random.randint(1,100)
            randomHitDamage = math.ceil(randomHIT * hitFactor)
            enemyHP -= randomHitDamage

            if enemyHP < 0:
                enemyHP = 0

            print("You hit the enemy for ", randomHitDamage," damage.",sep='')
            print("Enemy's HP: ", enemyHP)

            if enemyHP == 0:
                file = open("wonMessage.txt", "r")
                content = file.read()
                print(content)
                over = True
                winner()

        isHitCalc2 = random.randint(1,10)
        if isHitCalc2 > 3:
            isHitMe = True
        else:
            isHitMe = False
            print("Your enemy missed!")

        if isHitMe == True:
            randomHitMe = random.randint(1,100)
            randomHitDamageMe = math.ceil(randomHitMe * enemyHitFactor)
            yourHP -= randomHitDamageMe

            if yourHP < 0:
                yourHP = 0

            print("The enemy hit you for ", randomHitDamageMe, " damage.", sep='')
            print("Your HP: ", yourHP)

            if yourHP == 0:
                file = open("lostMessage.txt", "r")
                content = file.read()
                print(content)
                over = True

# Writes winner's name to text file           
def winner():
    winner = str(input("Please enter your name: "))
    infile = open("winner.txt", "w")
    infile.write("Latest winnner's name: ")
    infile.write(winner)

# Calls main
main()

2 个答案:

答案 0 :(得分:3)

这可能是因为您在

中致电winner()之后
 if enemyHP == 0:
      file = open("wonMessage.txt", "r")
      content = file.read()
      print(content)
      over = True
      winner()

你不会立即break退出循环。

相反,在进行while循环检查(over != True)之前,您仍然会继续处理下面的逻辑:

    isHitCalc2 = random.randint(1,10)
    if isHitCalc2 > 3:
        isHitMe = True
    else:
        isHitMe = False
        print("Your enemy missed!")

    if isHitMe == True:
        randomHitMe = random.randint(1,100)
        randomHitDamageMe = math.ceil(randomHitMe * enemyHitFactor)
        yourHP -= randomHitDamageMe

        if yourHP < 0:
            yourHP = 0

        print("The enemy hit you for ", randomHitDamageMe, " damage.", sep='')
        print("Your HP: ", yourHP)

        if yourHP == 0:
            file = open("lostMessage.txt", "r")
            content = file.read()
            print(content)
            over = True

您可以通过添加以下内容来修复此特定情况:

if enemyHP == 0:
      file = open("wonMessage.txt", "r")
      content = file.read()
      print(content)
      over = True
      winner()
      break

答案 1 :(得分:0)

如果在函数内调用函数,一旦完成第二个函数,它将继续执行第一个函数:

>>> import time
>>> def repeat():
...     num = 0
...     while True:
...             num+=1
...             print num
...             time.sleep(1)
...             if num == 10:
...                     stop()
... 
>>> def stop():
...     print 'This program has stopped!'
...     time.sleep(1)
... 
>>> repeat()
1
2
3
4
5
6
7
8
9
10
This program has stopped!
11
12
13
14
15
^CTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in repeat
KeyboardInterrupt
>>> 

num到达10后,它会调用stop(),但一旦stop()完成,它就会继续循环。而是在调用函数后添加break或空白return

>>> import time
>>> def repeat():
...     num = 0
...     while True:
...             num+=1
...             print num
...             time.sleep(1)
...             if num == 10:
...                     stop()
...                     break
... 
>>> def stop():
...     print 'This program has stopped!'
...     time.sleep(1)
... 
>>> repeat()
1
2
3
4
5
6
7
8
9
10
This program has stopped!
>>> 

在您的代码中:

# Imports modules
import random
import math

# Welcome message
def main():

    print("//////////////////////////////////////")
    print("//////////////////////////////////////")
    print("// Welcome Ninja, Pirate, or Zombie //")
    print("//////////////////////////////////////")
    print("//////////////////////////////////////")
    print("\n\n")

    choose()

# Prompts user to choose class
def choose():
    choice = str(input("Make a choice! (Ninja, Pirate, or Zombie) "))

    choice = choice.lower()

    if choice == "ninja" or choice == "pirate" or choice == "zombie":
        enemy(choice)
    else:
        choose()

# Randomly selects opposing enemy
def enemy(choice):

    enemyRandom = random.randint(1,3)

    if enemyRandom == 1:
        enemy = "ninja"
    elif enemyRandom == 2:
        enemy = "pirate"
    elif enemyRandom == 3:
        enemy = "zombie"
    else:
        print("Something went wrong!")

    hit_factor(choice, enemy)

# Determines the hit factor. Certain class are weak or strong when fighting certain
# other classes
def hit_factor(choice, enemy):

    if choice == "ninja" and enemy == "ninja":
        hitFactor = 1
    elif choice == "ninja" and enemy == "pirate":
        hitFactor = 1.2
    elif choice == "ninja" and enemy == "zombie":
        hitFactor = 0.8
    elif choice == "pirate" and enemy == "ninja":
        hitFactor = 0.8
    elif choice == "pirate" and  enemy == "pirate":
        hitFactor = 1
    elif choice == "pirate" and enemy == "zombie":
        hitFactor = 1.2
    elif choice == "zombie" and enemy == "ninja":
        hitFactor = 1.2
    elif choice == "zombie" and enemy == "pirate":
        hitFactor = 0.8
    elif choice == "zombie" and enemy == "zombie":
        hitFactor = 1
    else:
        print("Something went horribly wrong.")

    enemy_hit_factor(choice, enemy, hitFactor)

# Determines the enemy's hit factor
def enemy_hit_factor(choice, enemy, hitFactor):

    if enemy == "ninja" and choice == "ninja":
        enemyHitFactor = 1
    elif enemy == "ninja" and choice == "pirate":
        enemyHitFactor = 1.2
    elif enemy == "ninja" and choice == "zombie":
        enemyHitFactor = 0.8
    elif enemy == "pirate" and choice == "ninja":
        enemyHitFactor = 0.8
    elif enemy == "pirate" and choice == "pirate":
        enemyHitFactor = 1
    elif enemy == "pirate" and choice == "zombie":
        enemyHitFactor = 1.2
    elif enemy == "zombie" and choice == "ninja":
        enemyHitFactor = 1.2
    elif enemy == "zombie" and choice == "pirate":
        enemyHitFactor = 0.8
    elif enemy == "zombie" and choice == "zombie":
        enemyHitFactor = 1
    else:
        print("Something went horribly wrong.")

    combat(choice, enemy, hitFactor, enemyHitFactor)

# Initiates combat
def combat(choice, enemy, hitFactor, enemyHitFactor):

    yourHP = 1000
    enemyHP = 1000

    print("Your HP: ", yourHP)
    print("Enemy's HP: ", enemyHP)

    over = False

    while over != True:
        isHitCalc = random.randint(1,10)
        if isHitCalc > 3:
            isHit = True
        else:
            isHit = False
            print("You missed!")

        if isHit == True:
            randomHIT = random.randint(1,100)
            randomHitDamage = math.ceil(randomHIT * hitFactor)
            enemyHP -= randomHitDamage

            if enemyHP < 0:
                enemyHP = 0

            print("You hit the enemy for ", randomHitDamage," damage.",sep='')
            print("Enemy's HP: ", enemyHP)

            if enemyHP == 0:
                file = open("wonMessage.txt", "r")
                content = file.read()
                print(content)
                over = True
                winner()
                return

        isHitCalc2 = random.randint(1,10)
        if isHitCalc2 > 3:
            isHitMe = True
        else:
            isHitMe = False
            print("Your enemy missed!")

        if isHitMe == True:
            randomHitMe = random.randint(1,100)
            randomHitDamageMe = math.ceil(randomHitMe * enemyHitFactor)
            yourHP -= randomHitDamageMe

            if yourHP < 0:
                yourHP = 0

            print("The enemy hit you for ", randomHitDamageMe, " damage.", sep='')
            print("Your HP: ", yourHP)

            if yourHP == 0:
                file = open("lostMessage.txt", "r")
                content = file.read()
                print(content)
                over = True

# Writes winner's name to text file           
def winner():
    winner = str(input("Please enter your name: "))
    infile = open("winner.txt", "w")
    infile.write("Latest winnner's name: ")
    infile.write(winner)

# Calls main
main()

这基本上会在此代码中的return之后添加winner()

if enemyHP == 0:
    file = open("wonMessage.txt", "r")
    content = file.read()
    print(content)
    over = True
    winner()
    return
相关问题