-2

it's not breaking and I don't know why This is the problem section of the code. It is working fine, the try except is fine, and the check if not in range's break is working fine. But not the second one.

It checks if the input's in range, it's in range, it prints the input. It doesn't break. It continues instead. It then miraculously works the second time you input within the range.

import random
random.seed(10)
def userInput():
    while True:
        try:
            userInput=int(input('Type 1 for rock, 2 for scissors, or 3 for paper: '))
            while userInput not in range(1,4,1):
                print('Invalid input')
                break
        except:
            print('Invalid input')
            continue
        if userInput in range(1,4,1):
            print(userInput)
            break
    if userInput==1:
        return 'rock'
    if userInput==2:
        return 'scissors'
    if userInput==3:
        return 'paper'
  
def convertChoiceToString(compChoice):
    if compChoice==1:
        return 'I choose rock'
    elif compChoice==2:
        return 'I choose scissors'
    elif compChoice==3:
        return 'I choose paper'
#def rockPaperScissors():
    
def main():
    print('Rock Paper Scissors')
    userInput()
    print('You chose',userInput())
    compChoice=random.randrange(1,3,1)
    print(convertChoiceToString(compChoice))
main()
1
  • 3
    Please don't send code as an image. Please include the minimal code block as text within your narrative for others to more easily test and/or replicate your issue. Commented Oct 23 at 1:47

2 Answers 2

4

You called userInput() twice instead of saving and using return value of first call.

    userInput() # called userInput() and return value discarded
    print('You chose',userInput()) # called userInput() again and return value printed

Can be fixed like this.

def main():
    print('Rock Paper Scissors')
    userHand = userInput()
    print('You chose', userHand)
    # ...

(not an answer. I didn't check original code was working while writing this)

break at line 10 escapes while userInput...: on line 8, not outermost while True:.
Seems it should be if, not while.

def userInput():
    while True:
        try:
            userInput = int(input('rock=1, scissors=2, paper=3: '))
            if userInput not in [1, 2, 3]:
                print('invalid')
                continue
            break
        except:
            print('invalid')
            continue
    print(userInput)
    # ...
Sign up to request clarification or add additional context in comments.

2 Comments

and using range(1,4,1) on flag values (= won't be used numerically) doesn't seems like a good idea(especially for readability). Use list literal like [1,2,3], or try Enum if you need to handle numbers of flag values while making RPS-101 instead of normal rock-scissors-paper.
Even better, test the input without initially converting to int. Thus, if the input is either "1", "2" or "3" then you can convert to int with impunity
0

In your main() function you are calling userInput() twice. You should call it once and save its return value.

It's also worth noting that your input should be either 1, 2 or 3 but not necessarily as an integer. You could just validate and utilise as a string.

You also have a lot of repetition of string values. By using a dictionary you can conveniently avoid constant repetition and also simplify validation.

Something like this:

from random import choice

KEYS = "123"
OBJECTS = ["rock", "paper", "scissors"]
RPS = dict(zip(KEYS, OBJECTS))
KVS = [f"{k} for {v}" for (k, v) in RPS.items()]
PROMPT = "Type {}, {} or {}: ".format(*KVS)

def user_input() -> str:
    while True:
        ui = input(PROMPT)
        try:
            return RPS[ui]
        except KeyError:
            print("\aInvalid input")
        

def convert_choice_to_string(c: str) -> str:
    return "I choose " + RPS[c]

def main():
    print(*map(str.title, OBJECTS))
    ui = user_input()
    print("You chose", ui)
    comp_choice = choice(KEYS)
    print(convert_choice_to_string(comp_choice))

main()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.