简体   繁体   English

如何从用户那里获取输入,将它们放入列表中,然后从中随机选择一个,而无需多次询问

[英]How to get input from user, put them into a list and then choose a random one from them without having to ask multiple times

It's my 4th day learning programming so I'm very new to it.这是我学习编程的第 4 天,所以我对它很陌生。 and I'm trying to get the user's info(hobbies) in one question and save them into a list then give back the user one of the hobbies they chose.并且我试图在一个问题中获取用户的信息(爱好)并将它们保存到列表中,然后将他们选择的一种爱好还给用户。 this what I've came up with这是我想出的

import random

fav_hobbies = []
hobbies = input("what are your favourite hobbies?(cooking, writing,ect) ").lower()
fav_hobbies.append(hobbies)

situation = input("so are you bored now?(answer with yes or no): ").lower()
if situation == "yes":
     print("you should try " + random.choice(fav_hobbies))

elif boredom_situation == "no":
     print("awesome! have a nice day!")

The problem is that instead of choosing a word among the words that user has chosen it just prints all of the things they said.问题在于,它不是在用户选择的单词中选择一个单词,而是打印他们所说的所有内容。

How do I fix this?我该如何解决?

You are just accepting a string from the User and storing it as it is without splitting.您只是接受来自用户的字符串并按原样存储它而不拆分。

Assuming that you are accepting a space separated input , You can do this in:假设您接受空格分隔的 input ,您可以在以下位置执行此操作:

fav_hobbies = list(input("what are your favourite hobbies?(cooking, writing,ect) ").lower().split())
import random

fav_hobbies = list(input("what are your favourite hobbies?(cooking, writing,ect) ").lower().split())

situation = input("so are you bored now?(answer with yes or no): ").lower()
if situation == "yes":
     print("you should try " + random.choice(fav_hobbies))

elif boredom_situation == "no":
     print("awesome! have a nice day!")

If you use some other separator like , you can just add it to split(',')如果您使用其他一些分隔符,例如,可以将其添加到split(',')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多个列表中随机选择一个列表 python - How to choose one list random from multiple lists python 从一个函数获取列表和输入,然后在其他函数中运行它们 - get the list and input from one function and run them in different function 如何从 pastebin.com 获取句子并将它们放入列表中? - How to get sentences from pastebin.com and put them into a list? 如何使用 `return` 从 for 循环中取回多个值? 我可以把它们放在一个列表中吗? - How can I use `return` to get back multiple values from a for loop? Can I put them in a list? 如何从数据框中选择和选择列并将其放入新列? - How to pick and choose columns from dataframes and put them into new ones? 从许多 html 表中获取用户的输入,并使用 flask 和 panda 将它们存储在 xlsx 文件中(无需下载文件) - Get user's input from many html tables and store them in a xlsx file (without having to download the file) with flask and panda 从嵌套在列表中的每个列表中选择一个随机项,它们之间有所不同,然后生成一个列表n次(重采样) - Select one random item from each list nested in a list, different between them, and generate a list n times (RESAMPLING) 如何从用户那里获取输入并将其保存在列表中(Python Kivy)? - How to get inputs from user and save them in a list(Python Kivy)? 如何从用户输入中分离奇数并将它们放在同一行上? - How to separate odd numbers from a user input and put them on the same line? 如何组合来自多个文件的多行并将它们放入数组 - how to combine multiple lines from multiple files and put them to an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM