Help with if and variables

Joined
Nov 23, 2023
Messages
1
Reaction score
0
# I'm unsure how to write this I want to ask for an input then do something based on the input for example.


def ask():
ask = input("type y/n: ")
ask == "Y" or "y"
if ask != "Y" or "y":
print("problem")

# this always prints "problem" no matter what why?
 
Joined
Jul 4, 2023
Messages
357
Reaction score
41
You can use list for example in that way
[ code on-line ]
Python:
def ask():
    ask = input('type y/n: ')
  
    if ask in ['Y', 'y']:
        print('Typed y')
      
    if ask in ['N', 'n']:
        print('Typed n')
      
    if not ask in ['Y', 'y', 'N', 'n']:
        print(f'Typed: {ask}')
      
ask()

or
[ code on-line ]
Python:
def ask():
    ask = input('type y/n: ')
   
    if ask in ['Y', 'y']:
        print('Typed y')
    elif ask in ['N', 'n']:
        print('Typed n')
    else:
        print(f'Typed: {ask}')
       
ask()



BTW,
[ code on-line ]
Python:
def ask():
    ask = input('Type y/n: ')
    if ask.lower() != 'y' and ask.lower() != 'n':
        print('Problem', ask)

ask()
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top