我盡力在不同的論壇上找到它,如果它很容易,請原諒。
我想從用戶那里獲取不同的輸入并將它們分配給不同的變數。使用 split() 我們只能在一行中輸入輸入。
w,x,y,z = (input('Enter the number :').split(','))
但是我們如何在不同的行中請求輸入并將它們分配給變數而無需再次輸入輸入函式。
我想要與下面的代碼相同的輸出,但不需要多次輸入輸入函式。
w= int(input('Enter the number :'))
x= int(input('Enter the number :'))
y= int(input('Enter the number :'))
z= int(input('Enter the number :'))
uj5u.com熱心網友回復:
一種解決方案可以將 4 個數字輸入到陣列中,然后將它們分配給變數。例如:
numbers = [int(input("Enter the number : ")) for _ in range(4)]
w, x, y, z = numbers
print(f"{w=} {x=} {y=} {z=}")
印刷:
Enter the number : 2
Enter the number : 3
Enter the number : 4
Enter the number : 5
w=2 x=3 y=4 z=5
或者:
w, x, y, z = [int(input("Enter the number : ")) for _ in range(4)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/470666.html
標籤:python-3.x for循环 用户输入