简体   繁体   中英

How to obtain user input in Python?

I would like to learn how to code this.

Here is how I want it to look

Do you want strawberry ice cream? (y:n) n
Do you want chocolate ice cream?  (y:n) y
Do you want mint ice cream? (y:n) n
Do you want vanilla ice cream? (y:n) y

then the output would be: Here is your ice cream. or if you put N to all, You did not say yes to any. Or with the else statement: I didn't get that, try again.

Any help is very much appreciated, thank you.

My test script:

def kind(chosenIce):
      chosenIce=input("Do you want a ice cream (y:n) ")
      if chosenIce1 == 'y':
          ice1 = print("message")
      if chosenIce2 == 'y':
          ice2 = print("message")
      if chosenIce1 == 'n':
          ice1 = 0
      if chosenIce2 == 'n':
          ice2 = 0
      else:
         print("Sorry, I did not get that. Try again.")
kind(chosenIce)

Regarding only what you want in this problem, you will use the function input . The way to use it is this:

strawberryIceCream=input('Do you want strawberry ice cream? (y/n)')

and so on, so forth with the other options as well. Remember to store each option in either a different variable or, better IMHO, in a set or tuple.

Further info on input

However, the comments' on your question are accurate and on point. This is why I mentioned that this answer will answer only your question. Nothing more, nothing less.

As some fellows said, you have to use input and loop (ie for loop or while loop ) to achieve your goal. By reading your post and the comments, it seems you are really a newbie in Python. So, below, I give you a basic workflow that does something like your needs. There are many ways to achieve your goals. You can look at my workflow for inspiration.

Here is the code:

def kind(ices_cream):
    choices = []
    for ice in ices_cream:
        chosenIce = input('Do you want "{}" ice cream (y:n) '.format(ice))
        if chosenIce == 'y':
            choices.append(ice)
        elif chosenIce == 'n':
            print("You refuse ", ice)
        else:
            print("Sorry, I did not get that. Try again.")

    if len(choices):
        print("\n", "You buy", ' and '.join(choices))
    else:
        print("\n", "You buy nothing!")

if __name__ == "__main__":
    ices_cream = ["Chocolate", "Strawberry", "Mint", "Vanilla"]
    kind(ices_cream)

Outputs: It is an example

Do you want "Chocolate" ice cream (y:n) y
Do you want "Strawberry" ice cream (y:n) n
You refuse  Strawberry
Do you want "Mint" ice cream (y:n) y
Do you want "Vanilla" ice cream (y:n) n
You refuse  Vanilla

 You buy Chocolate and Mint

NB: You can add a loop while to force the user to input the characters y or n . Also, you can add exception handling if you want to do a good job.

listQue=["Do you want strawberry ice cream?","Do you want chocolate ice cream?","Do 
you want mint ice cream?","Do you want vanilla ice cream?"]

flag=False
for que in listQue:
    ans=input(que)
    if(ans=='Y'):
        res=que.split()
        print("Here is your"," ".join(res[3:6]).rstrip('?'))
        flag=True
   else:
        pass


if(flag==False):
    print("Ohhh sorry you didn't choose anything")

In python 3 you can use input() (examples here ) and in python 2 raw_input() (examples here ). You need to code the rest yourself using for-loops... etc.

Example:

choices = []

x1 = input('Do you want strawberry ice cream? (y/n)')

choices.append(x1)  # store the choice for each question here so you can use it later for
                # selection criteria process. Store x2 at choices[1], etc.

for choice in choices:
    if choice in ['y', 'Y', 'yes', YES']:
        print ('User does like an iscream')
    else:
        print ('User no fan of the cold treat')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM