【问题标题】:Multiple inputs from a user in a single line for pythongpython的单行中来自用户的多个输入
【发布时间】:2020-06-24 09:43:42
【问题描述】:

使用 python 你有 input() 它需要一个用户输入,您可以将其分配给一个变量,然后做您想做的事。 我听说过 .split() 方法,但不知道如何在我的代码中实现它。 我想创建一个使用 def() 的函数,这样我可以多次回调该函数,同时询问用户是否要完成该过程、结束它或执行另一个函数

print("Hello, welcome to the change calculator")
print("Here we will ask you to tell us the amount of each individual coins you have")
#create variables that hold the amount each one states such as five pence would == £0.05
fivepence = 0.05
tenpence = 0.10
twentypence = 0.20
fiftypence = 0.50
onepound = 1
twopound = 2
#Get the user to input everything 



fivepencetotal = input(("How many 5 Pence coins do you have? : ")) * fivepence

tenpencetotal = input(("How many 10 Pence coins do you have? : ")) * tenpence

twentypencetotal = input(("How many 20 Pence coins do you have? : ")) * twentypence

fiftypencetotal = input(("How many 50 pence coins do you have? : ")) * fiftypence

onepoundtotal = input(("How many 1 Pound coins do you have? : ")) * onepound

twopoundtotal = input(("How many 2 pound coins do you have? : ")) * twopound


#function to add everything and do the opposite if that is what the user wants

def changeaddup():
    print("do you want to add everything up?")
    print("Type either Yes, yes , Y or y to add all your change up")
    if input( "Yes","yes","Y","y"):
        print("Just calculating how much money you have based on the data you have inputed")
        print("Here you have a total of £", fivepencetotal + tenpencetotal + twentypencetotal + fiftypencetotal + onepoundtotal + twopoundtotal)
        
    else:
        return
print(changeaddup)

【问题讨论】:

  • 完成这个过程是什么意思?你的问题到底是什么?
  • 希望对您有所帮助:stackoverflow.com/questions/61908027/…
  • 哦,是的,对不起,我很抱歉,我明白您将输入转换为列表的意思,我希望用户能够输入多位信息,例如“是”“是”“ y" "Y"

标签: python python-3.x multiple-input


【解决方案1】:

不完全知道你想要什么,但可能是这样的

positive_choice_options = ["Yes","yes","Y","y"]

# and there can be other choice if you need. ie.
negetive_choice_options = ["No", "no", "N", "n"]

def normalized_user_choice(user_choice):
    if user_choice in positive_choice_options:
        return "yes"
    if user_choice in negetive_choice_options:
        return "no"

def changeaddup():
    print("do you want to add everything up?")
    user_choice = input("Type either Yes, yes , Y or y to add all your change up: ")
    if normalized_user_choice(user_choice)=="yes":
        print("Just calculating how much money you have based on the data you have inputed")
        print("Here you have a total of £", fivepencetotal + tenpencetotal + twentypencetotal + fiftypencetotal + onepoundtotal + twopoundtotal)
    else:
        return

【讨论】:

  • 这实际上回答了所有问题,谢谢我现在通过创建数组来理解它,因此用户输入有多种选择,谢谢。
猜你喜欢
  • 2017-03-04
  • 1970-01-01
  • 2011-08-30
  • 2016-04-21
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多