Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is My While Loop Acting Like an Input

I am fairly new to python and am currently making a tic-tac-toe game.

I am trying to make it so if your input does not equal rock, paper, or scissors it will prompt you to re enter until you get it right, although when I run the code it asks you to re enter one time and then continues on, regardless of what you enter.

I have tried looking around the internet but nothing seems to be what I am looking for, here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import random

print("How many times would you like to play?")
repeat = input()
possible_choices = ["rock", "paper", "scissors"]
for x in range (int(float(repeat))):
  user_input = input("Enter a choice (rock, paper, scissors): ")
  if user_input != possible_choices:
    user_input = False
  else:
    user_input = True
  
  while user_input == False: 
    user_input = input("Please enter rock, paper, or scissors: ")
    if user_input == possible_choices:
      user_input = True
  
      
  computer_action = random.choice(possible_choices)
  print(f"\nYou chose {user_input}, computer chose {computer_action}.\n")
  if (computer_action == user_input):
    print(f"You both chose {user_input} it's a tie!")

  if (user_input == "rock") and (computer_action == "paper"):
    print("Paper beats rock, you lose!")

  if (user_input == "rock") and (computer_action == "scissors"):
    print("Rock beats scissors, you win!")

  if (user_input == "paper") and (computer_action == "scissors"):
    print("Scissors beats paper, you lose!")

  if (user_input == "scissors") and (computer_action == "paper"):
    print("Scissors beats paper, you win!")
    print("")

  if (user_input == "paper") and (computer_action == "rock"):
    print("Paper beats rock, you win!")
  
print("Game ended.")

>Solution :

You redefine user_input inside the for loop. Since a string will evaluate to not False the loop will always break. Another thing, you should be checking if the input is in the possible choices, not equal to the whole list.

for x in range (int(repeat)):
    user_input = input("Enter a choice (rock, paper, scissors): ")
    valid_input = False
    if user_input in possible_choices:
        valid_input = True
    while valid_input == False: 
        user_input = input("Please enter rock, paper, or scissors: ")
        if user_input in possible_choices:
            valid_input = True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading