简体   繁体   English

修复一个while循环只运行一次

[英]Fixing a while-loop to only run once

I am trying to create a simple Rock, Paper, Scissors game to practice some basic Python skills.我正在尝试创建一个简单的石头剪刀布游戏来练习一些基本的 Python 技能。 I created the code below to take user input and create a simple point-based game.我创建了下面的代码来接受用户输入并创建一个简单的基于点的游戏。 However, when I ran the code, the loop ran infinitely, printing the output message until I force stopped it.但是,当我运行代码时,循环无限运行,打印 output 消息,直到我强制停止它。 How can I alter my code so the while-loop only runs once per user input?如何更改我的代码,以便每个用户输入的 while 循环只运行一次?

code:代码:

import random as rand

print("Welcome to Rock, Paper, Scissors!")

x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")

game_choices = ("Rock",
                "Paper",
                "Scissors")

comp_input = rand.choice(game_choices)

phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"

my_score = 0

print("The computer chose: ",comp_input)

while my_score >= 0:                                            
    if x == "R" and comp_input == "Rock":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Scissors":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Paper":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Scissors":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Paper":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Rock":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Paper":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Rock":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Scissors":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break

You need to set your while loop before the user input, otherwise it is just looping over and over the same inputs values:您需要在用户输入之前设置 while 循环,否则它只是循环遍历相同的输入值:

import random as rand

print("Welcome to Rock, Paper, Scissors!")

my_score = 0

while my_score >= 0:   

    x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")

    game_choices = ("Rock",
                    "Paper",
                    "Scissors")

    comp_input = rand.choice(game_choices)

    phrase_one = "It was a tie!"
    phrase_two = "You win! WOOOOOOO go Grandma!!!"
    phrase_three = "You lost.. shoulda had a V8!"

    print("The computer chose: ",comp_input)
                                         
    if x == "R" and comp_input == "Rock":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Scissors":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Paper":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Scissors":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Paper":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Rock":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Paper":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Rock":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Scissors":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break

Below you get a little more compact version by the way:顺便说一下,下面你会得到一个更紧凑的版本:

import random as rand

print("Welcome to Rock, Paper, Scissors!")

phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"

my_score = 0

while my_score >= 0:   

    x = input("Your move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ")

    game_choices = ("Rock",
                    "Paper",
                    "Scissors")

    comp_input = rand.choice(game_choices)

    print("The computer chose: ",comp_input)

    #You store the choices in the same list
    match = [x[0],comp_input[0]]

    #set operation provides you with unique elements
    #If the user and comp inputs char are the same, then length = 1
    if len(set(match)) == 1:
        print(phrase_one)
        print("Score: ",my_score)
    #If match in combinations that makes the user win
    elif match in [['R','S'],['S','P'],['P','R']]:
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
    #Other situations are equivalent to loose
    else:
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")

    #You don't need break statements in your code, btw (while condition)

Here, if user choice is "Rock" and computer choice "Paper", you store the value for the first index of each string in a list called match.在这里,如果用户选择“Rock”而计算机选择“Paper”,则将每个字符串的第一个索引的值存储在名为 match 的列表中。

choice_1 = "Rock"
choice_2 = "Paper"

match = [choice_1[0],choice_2[0]] # match = ["R","P"]

match = set(match) # Yield match = ["R","P"], to test against winning/loosing combinations

choice_1 = "Rock"
choice_2 = "Rock"

match = [choice_1[0],choice_2[0]] # match = ["R","R"]

match = set(match) # Yield match = ["R"], it is a tie because len(match) = 1

the problem is that unless you loose on your first round you keep winning for ever because none of the input values changed.问题是,除非您在第一轮中输掉比赛,否则您将永远获胜,因为输入值都没有改变。 all you have to do is move your input() and the computers random selection into the loop like this您所要做的就是将您的 input() 和计算机随机选择移动到这样的循环中

import random as rand

print("Welcome to Rock, Paper, Scissors!")
game_choices = ("Rock",
                    "Paper",
                    "Scissors")



phrase_one = "It was a tie!"
phrase_two = "You win! WOOOOOOO go Grandma!!!"
phrase_three = "You lost.. shoulda had a V8!"

my_score = 0
while my_score >= 0:
    
    x = input("\nYour move first. Press 'R' for rock, 'P' for paper, or 'S' for scissors: ").upper()
    comp_input = rand.choice(game_choices)

    print("The computer chose: ",comp_input)

                                            
    if x == "R" and comp_input == "Rock":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Scissors":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Paper":
        print(phrase_one)
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Scissors":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "R" and comp_input == "Paper":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Rock":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "S" and comp_input == "Paper":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Rock":
        print(phrase_two)
        my_score += 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break
    elif x == "P" and comp_input == "Scissors":
        print(phrase_three)
        my_score -= 1
        print("Score: ",my_score)
        if my_score < 0:
            print("Game over :( Also, YOU SUCK!")
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM