簡體   English   中英

pythong的單行中來自用戶的多個輸入

[英]Multiple inputs from a user in a single line for pythong

使用 python 你有 input() 它需要一個用戶輸入,你可以將它分配給一個變量,然后做你想做的事。 我聽說過 .split() 方法,但不知道如何在我的代碼中實現它。 我想創建一個 function 使用 def() 以便我可以多次回調 function,同時詢問用戶是否要完成該過程、結束它或執行另一個 ZC1C425274E68384F1AB4ZA

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)

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM