Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Pre-set options

Hey, really out of practice (even with i'd still be novice)

Wanted to have preset options that the user can pick and have 3 additional preset options come up. Format may be a little wonky since I did this in Pycharm for it to run properly:

print("Welcome!")
print("How do you want to start today?")
input()
if input() == "Option 1"
print(Sub_option 1, Sub_option 2, Sub_option 3)
else input() == "Option 2"
print(Sub_option 1, Sub_option 2, Sub_option 3)
input()

** Wasnt too sure if I could fit another elsif statement into it, like I would for any other container
 
Maybe something like this

Python:
print('Welcome')
print('How to start')
print('Enter either 1 or 2 and press enter')
# Usinga try except block to ensure an integer is entered
# If not throw an error
try:
    # Option will be a whole number so use int around input
    option = int(input('>> '))
   
    if option == 1:
        print('You chose option 1')
    elif option == 2:
        print('You chose option 2')
    else:
        print('Some other option')
except ValueError as err:
    print('Invalid character. Please enter a whole number.')

If there are going to be a lot of options or you are calling functions, might be better to use a dict or list to store choices.
 
Maybe something like this

Python:
print('Welcome')
print('How to start')
print('Enter either 1 or 2 and press enter')
# Usinga try except block to ensure an integer is entered
# If not throw an error
try:
    # Option will be a whole number so use int around input
    option = int(input('>> '))
  
    if option == 1:
        print('You chose option 1')
    elif option == 2:
        print('You chose option 2')
    else:
        print('Some other option')
except ValueError as err:
    print('Invalid character. Please enter a whole number.')

If there are going to be a lot of options or you are calling functions, might be better to use a dict or list to store choices.
Yeah theres going to be a bunch of options, ill input the list first, then fix the code
 
An example of how a dict might be used. Keep in mind that there is no kind of error checking or validation other than checking for a whole number.

Python:
def option1():
    return 'sub-options for option 1'

def option2():
    return 'sub-options for option 2'

option_dict = {
1: option1(),
2: option2()
}

print('Welcome')
print('How to start')
print('Enter either 1 or 2 and press enter')
# Usinga try except block to ensure an integer is entered
# If not throw an error
try:
    # Option will be a whole number so use int around input
    option = int(input('>> '))

    # Loop through dict and execute a function if key == option
    for key, value in option_dict.items():
        if option == key:
            print(value)
# Throw an error if invalid character entered
except ValueError as err:
    print('Invalid character. Please enter a whole number.')
 
An example of how a dict might be used. Keep in mind that there is no kind of error checking or validation other than checking for a whole number.

Python:
def option1():
    return 'sub-options for option 1'

def option2():
    return 'sub-options for option 2'

option_dict = {
1: option1(),
2: option2()
}

print('Welcome')
print('How to start')
print('Enter either 1 or 2 and press enter')
# Usinga try except block to ensure an integer is entered
# If not throw an error
try:
    # Option will be a whole number so use int around input
    option = int(input('>> '))

    # Loop through dict and execute a function if key == option
    for key, value in option_dict.items():
        if option == key:
            print(value)
# Throw an error if invalid character entered
except ValueError as err:
    print('Invalid character. Please enter a whole number.')
If I wanted to use something other than an integer for option would this still be reccomended? I figured add like option 1 being first food choice or option 2 being second food choice
 
In my opion using dicts or list is best for alot of options. Again that's my pref. Numbers can be changed to text. As I said before dicts take a key, value pair.
so options could be like

Python:
dict_options = {
    'hamburger': option1(),
    'hotdog': option2()
}

The code would need to be written a little different.
 
In my opion using dicts or list is best for alot of options. Again that's my pref. Numbers can be changed to text. As I said before dicts take a key, value pair.
so options could be like

Python:
dict_options = {
    'hamburger': option1(),
    'hotdog': option2()
}

The code would need to be written a little different.
Thank you, going to take another crack at it
 
Back
Top Bottom