(Python)如何使用'n'可变次数循环某个代码区域

时间:2017-09-25 01:08:00

标签: python

def player_choice():

    while True:

        roundsplayed = int(input("Choose how many rounds you want to play from 1 to 5! "))

        if roundsplayed < 1 or roundsplayed > 5:
            print ("Please enter a valid number from 1 to 5! ")
            continue
        return player_choice
    else:
        print ("Lets play " + roundsplayed + "rounds! ")

roundsplayed = player_choice()

print ("Let's go go go!")




options = ("r", "p", "s", "l", "sp")
from random import randint
computer = options[randint(0,4)]

for i in range(roundsplayed):

    wins = 0
    loses = 0
    draws = 0

    player = input("""Choose your hand!
    Rock (r)
    Paper (p)
    Scissor (s)
    Lizard (l)
    Spock (s)
    Your Choice: """)



    # if player chooses rock
    if player == "r" and computer == "r":
        print ("You tied!")

    elif player == "r" and computer == "s":
        print ("You crushed them! You Win!")
        wins += 1
        print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

    elif player == "r" and computer == "p":
        print ("You got covered! You lose!")
        loses += 1
        print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

    elif player == "r" and computer == "l":
        print ("You crushed them! You Win!")
        wins += 1
        print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

    elif player == "r" and computer == "sp":
        print ("You got vaporized! You lose!")
        loses += 1
        print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

所以我有这行代码

for i in range(roundsplayed):

wins = 0
loses = 0
draws = 0

player = input("""Choose your hand!
Rock (r)
Paper (p)
Scissor (s)
Lizard (l)
Spock (s)
Your Choice: """)



# if player chooses rock
if player == "r" and computer == "r":
    print ("You tied!")

elif player == "r" and computer == "s":
    print ("You crushed them! You Win!")
    wins += 1
    print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

elif player == "r" and computer == "p":
    print ("You got covered! You lose!")
    loses += 1
    print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

elif player == "r" and computer == "l":
    print ("You crushed them! You Win!")
    wins += 1
    print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

elif player == "r" and computer == "sp":
    print ("You got vaporized! You lose!")
    loses += 1
    print ("Wins: {} Draws: {} Loses: {}".format(wins, draws, loses))

并希望通过输入某个变量或输入来重复我希望它循环的次数。我把'roundsplayed'变量放在代码接受用户输入的变量的想法中。

显然这不起作用,因为代码没有将其定义为整数。

也适用于:

 wins = 0
loses = 0
draws = 0

我希望它在轮次重新开始循环时计算结果,但显然有问题,因为每当它选择重新启动循环时,计数器重置为0,很可能是由于明显的错误。

我希望有人可以帮助我,这就是我作为python学习者的编码需要解决的问题,以便我可以继续我的下一个项目。

2 个答案:

答案 0 :(得分:0)

将字符串转换为整数,

int("stringhere")

如果您不想在每次循环时重置变量,请将它们从循环中取出。

答案 1 :(得分:0)

为了在循环中使用变量而不重置它们,将它们声明在循环之上(循环之外)。

 wins = 0
    loses = 0
    draws = 0
for i in range(roundsplayed):


    player = input("""Choose your hand!
    Rock (r)
    Paper (p)
    Scissor (s)
    Lizard (l)
    Spock (s)
    Your Choice: """)

如果你想把你从控制台获得的字符串转换为int,你可以使用Int()函数。

var number = int("1")

https://docs.python.org/2/library/functions.html