用户输入编号并使用python中的循环分配给不同的变量

发布于 2025-01-26 22:20:24 字数 412 浏览 2 评论 0原文

我尽力在不同的论坛上找到它,所以请原谅我很容易。

我想从用户中获取不同的输入,并将其分配给不同的变量。 使用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 :'))

I tried my best to find it on different forums so excuse me if it is an easy one.

I want to take different inputs from the user and assign them to different variables.
Using split() we can only enter the inputs in one line.

w,x,y,z = (input('Enter the number :').split(','))

But how can we ask for inputs in different line and assign them to the variables without typing the input function again.

I want the same output as below code but without typing input function multiple times.

w= int(input('Enter the number :'))
x= int(input('Enter the number :'))
y= int(input('Enter the number :'))
z= int(input('Enter the number :'))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻想少年梦 2025-02-02 22:20:24

一个解决方案可以输入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)]

One solution can be input 4 numbers to array and then assign them to variables. For example:

numbers = [int(input("Enter the number : ")) for _ in range(4)]
w, x, y, z = numbers

print(f"{w=} {x=} {y=} {z=}")

Prints:

Enter the number : 2
Enter the number : 3
Enter the number : 4
Enter the number : 5
w=2 x=3 y=4 z=5

Or:

w, x, y, z = [int(input("Enter the number : ")) for _ in range(4)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文