Question How to make options/choices in Python?
What do you mean choices? Like text input, visual input or what? Could you please clarify?
Ok, so my character is asking why there disturbing them so I want to have a 1. 2. 3. options in Python.
Again, text or visual? Could you also provide a snippet of code where you want this choice?
As in I dont know how to control input of the person playing.
It is text choices, dont know how to do any visual yet.
print(f """
Pick a Choice!
[1]. First Choice
[2]. Second Choice
[3]. Third Choice \n
""")
choose = input("")
if choose == "1" :
print("Whatever")
if choose == "2" :
print("Doomsdaybear is awesome")
if choose == "3" :
print("Doomsdaybear is teh best h4x0r ever")
Got to it before me lol
Lol I was typing so fast so there weren’t two answers.
lol, thank you very much, guys.
Also, why is the first print an f string?
Don’t know, I just like f strings
To speed things up, especially if you have a lot of conditions and they are exclusive, use elif instead of a sequences of if’s
print("Choices: 1. you will die, 2. Chase the impostor off the ship, 3. get yeeted")
choice=int(input("enter a number (1,2,3):")) #make sure to use numbers with int
if choice ==1: print("goodbye")
elif choice ==2: print("They were not the impostor")
elif choice ==3: print("YEET")
hey i was going to comment that >:|
I still will:
print("""
Why are they disturbing you?:
[1] They are disturbing you because...
[2] They are disturbing you because...
[3] They are disturbing you because...
""")
choice = input("")
if choice == "1" :
print("Blablabla")
elif choice == "2" :
print("Yourtexthere")
elif choice == "3" :
print("MiloCat is the best kitten :D")
Mine has better syntax @anon45021817
This gives you an error, just remove the f.
Lol yeah i noticed that around 2 hours ago and forgot tp change it.
How to make a simple text choices in python
You can just do a simple print & input statement.
print("""
What is your favorite ice-cream flavor?
[1]. Chocolate
[2]. Vanilla
[3]. Strawberry
""")
choice = int(input(">"))
if choice == 1:
print("🍫")
elif choice == 2:
print("⬜")
elif choice == 3:
print("🍓")
Adding more features
If u wanna get more advanced, you can make it so that the user can change their choice by pressing ⬆
and ⬇
The simplest way to do it is by using the getkey
module and by moving the cursor using the ansi.cursor
module.
from sys import stdout
from getkey import getkey
from ansi.color import fg
from ansi import cursor
def menu(choices):
global pos
pointer = fg.boldblue('>')
for choice in enumerate(choices):
text = f' {choice[0]+1}. {choice[1]}'
if choice[0] == 0:
text = fg.bold(text)
print(text)
print(cursor.hide()+cursor.up()*(len(choices))+pointer, end='')
stdout.flush()
pos = 1
while 1:
key = getkey()
text = f' {pos}. {choices[pos-1]}'
if (key == '\x1b[A' or key == 'w') and pos > 1:
pos -= 1
print(f'\r{text}\r' + cursor.up() + pointer + \
fg.bold(f' {pos}. {choices[pos-1]}'), end = '')
elif (key == '\x1b[B' or key == 's') and pos < len(choices):
pos += 1
print(f'\r{text}\r' + cursor.down() + pointer + \
fg.bold(f' {pos}. {choices[pos-1]}'), end = '')
elif key == '\n':
print(cursor.down()*(len(choices)-pos)+cursor.show())
return pos
elif key.isdecimal():
number = int(key)
if 0 < number <= len(choices):
print(cursor.down()*(len(choices)-pos)+cursor.show())
return number
stdout.flush()
print('What is your favorite ice-cream flavor?')
choice = menu(['Chocolate', 'Vanilla', 'Strawberry'])
if choice == 1:
print("🍫")
elif choice == 2:
print("⬜")
elif choice == 3:
print("🍓")
(Source: https://replit.com/@MNA4/Python-terminal-menu-TEMPLATE#main.py)
I hope that helps
very nice I really love the aesthetics.