Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do I join two different input formats in Python?

+3 votes
asked Jan 6 by YC72909 (150 points)
Hello,

I could not find a way to input a number, and then use that number input to output a word. The numbers are 1, 2, and 3. The words are rock, paper, and scissors. I wanted to be able to tell the user to input a number (e.g. 1 for rock, 2 for paper, etc.) so that the program could output a word according to the numbers 1, 2, and 3. Also, once the user has inputted either a 1, 2, or 3, I do not know how to tell the program to Boolean check three user inputs. I tried using the function "and" but that did not work.

Thank you

1 Answer

0 votes
answered Jan 6 by Peter Minarik (66,630 points)

I hope you'll find the following code snipper with comments helpful:

# keep reading a string then convert it into a number as long as it is not successful
success = False
while not success:
    stringInput = input("Choose 1/2/3 for rock/paper/scissors: ")
    try:
        index = int(stringInput)
        if 1 <= index and index <= 3:
            success = True
        else:
            print("Invalid number")
    except ValueError:
        print("Not a number!")

# print the choice that was 1-based, but lists are 0-based
choice = ["rock", "paper", "scissors"]
print(f"You chose: {choice[index - 1]}")
commented Jan 7 by YC72909 (150 points)
Thank you for your help. The code worked very well.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...