Skip to content
Sign upLog in
← Back to Community

User enters number output as string

Profile icon
Divad31188

Hey guys, i know i've read it somewhere but how can I switch from a number to a Text in Python?
e.G. What did you wanne Eat tip 1 for Burger King and 2 for McDonalds, or something like thatπŸ€”πŸ˜…

Voters
Profile icon
Divad31188
Comments
hotnewtop
Profile icon
OrvalHammes

I like output as string because it's easy to use. I can just put the string in a file, and then import it into my program. Also, I can use it if I have multiple files with different data types. It tells you what the command is doing, so you can troubleshoot it and figure out why something is not working. Students should https://knowtechie.com/top-6-most-popular-tech-tools-in-the-uk/ to learn about the best tech tools. It also helps with debugging. If you want to know what a command does, just run it and look at its output.

Profile icon
InvisibleOne

Just convert the input to an integer with int()

print("How many burgers do you want to eat? ") answer = int(input()) if answer >= 10: print("That's too many!") elif answer == 1: print("Are you sure you don't want two?")

Just keep in mind that if they submit something other than a number, you will get an error, so maybe you want to try something like this:

# custom input function def int_input(prompt): while True: try: result = int(input(prompt)) if result: return result break else: print("You didn't give me an answer!") except: print("Please input an integer!")

And then when you want to get an integer input:

answer = int_input("Give me a number: ")
Profile icon
Coder100

You can convert a string to int: int('1')

You can convert a int to string: str(1)

but in your case, none of those are necessary, just use if statements!

var = input('what you want ') if var == '1': # ... elif var == '2': # ... else: print('what')
Profile icon
xfinnbar

If the number is decimal, use float(). If it is not decimal, use int().