重置输入 x 和 y 的数值如何操作?

huangapple go评论72阅读模式
英文:

How do I reset the values of input x and y?

问题

使用input()函数从控制台获取两个整数xy作为输入。对于每个赋值运算符(=, +=, -=, 和 *=),请将这些运算符应用于两个输入整数,并按示例显示结果。

假设:

  • y 是一个非零整数

提示: 在每次赋值操作之前,将x和y的值重置为初始值。

这是我的代码行:

x = int(input("x: "))
y = int(input("y: "))

x_temp = x  # 用于保存初始x的值
y_temp = y  # 用于保存初始y的值

x = y
print("x = y:", x)

x = x_temp  # 重置x为初始值
x += y
print("x += y:", x)

x = x_temp  # 重置x为初始值
x -= y
print("x -= y:", x)

x = x_temp  # 重置x为初始值
x *= y
print("x *= y:", x)

如何重置x和y的值到初始值?

示例输入和输出:

x: 20
y: 30
x = y: 30
x += y: 50
x -= y: -10
x *= y: 600
英文:

Take two integers x and y as inputs from the console using input() function. For each assignment operator (=, +=, -=, and *=), print to the console, the result of applying these operators on the two input integers as shown in the example.

Assumption:

  • y is a non-zero integer

Hint: Before every assignment operation, reset the values of x and y to the initial values.

This is my code line:

x = int(input("x: "))
y = int(input("y: "))

x = y
print("x = y:", x)

x += y
print("x += y:", x)

x -= y
print("x -= y:", x)

x *= y
print("x *= y:", x)

How do I reset the values of x and y to the initial values??

Sample Input and Output:

x: 20
y: 30
x = y: 30
x += y: 50
x -= y: -10
x *= y: 600

答案1

得分: 1

这是因为变量'x'在过程中不断变化,您可以将输入的x保存到另一个变量中,然后不断重置为这个值。

base_x = int(input("x: "))
y = int(input("y: "))

x = y
print("x = y:", x)
x = base_x

x += y
print("x += y:", x)
x = base_x

x -= y
print("x -= y:", x)
x = base_x

x *= y
print("x *= y:", x)
英文:

This is because the variable 'x' is constantly changing during the process, you can save the input x to another variable, and then reset to this value continuously

base_x = int(input("x: "))
y = int(input("y: "))

x = y
print("x = y:", x)
x = base_x

x += y
print("x += y:", x)
x = base_x

x -= y
print("x -= y:", x)
x = base_x

x *= y
print("x *= y:", x)

答案2

得分: 0

在这种情况下,你没有修改y,因为你只是重新赋值x。

我认为重新初始化x的唯一方法是在每次操作之前,将x=y赋值。

学习如何调试可以帮助你了解代码执行过程中发生了什么,以及变量如何被修改。

英文:

In this case you are not modyfing y since you are only reassigning x.

I think the only way to reinitialize x is that every time, before every operation, you assign x=y.

Learning how to debug can help you understand what's happening during the execution of the code and how the variables are modified

答案3

得分: 0

如果我理解正确,我假设这些初始值是输入值。所以最好使用初始值的副本执行这些操作(+,-,* 和 /),如以下代码:

x_initial = int(input("x: "))
y_initial = int(input("y: "))

x = x_initial 
y = y_initial

x = y
print("x = y:", x)

# 重置
x = x_initial 
y = y_initial

x += y
print("x += y:", x)

# 重置
x = x_initial 
y = y_initial

x -= y
print("x -= y:", x)

# 重置
x = x_initial 
y = y_initial

x *= y
print("x *= y:", x)
英文:

If I understand you correctly, I assume those initial values are the input values. So it is better to perform these operations (+, -, * and /) with copy of the initial values like the following code:

x_initial = int(input("x: "))
y_initial = int(input("y: "))

x = x_initial 
y = y_initial

x = y
print("x = y:", x)

# Reset
x = x_initial 
y = y_initial

x += y
print("x += y:", x)

# Reset
x = x_initial 
y = y_initial

x -= y
print("x -= y:", x)

# Reset
x = x_initial 
y = y_initial

x *= y
print("x *= y:", x)

答案4

得分: 0

获取初始输入,解包,然后在每次操作之前将x和y分配给初始值以进行重置。

init = int(input("x: ")), int(input("y: "))

x, y = init
x = y
print("x = y:", x)

x, y = init
x += y
print("x += y:", x)

x, y = init
x -= y
print("x -= y:", x)

x, y = init
x *= y
print("x *= y:", x)

# x: 20
# y: 30
# x = y: 30
# x += y: 50
# x -= y: -10
# x *= y: 600
英文:

Get intial input, unpack, then assign x and y to initial value before each operation to reset.

init = int(input("x: ")), int(input("y: "))

x, y = init
x = y
print("x = y:", x)

x, y = init
x += y
print("x += y:", x)

x, y = init
x -= y
print("x -= y:", x)

x, y = init
x *= y
print("x *= y:", x)

# x: 20
# y: 30
# x = y: 30
# x += y: 50
# x -= y: -10
# x *= y: 600

huangapple
  • 本文由 发表于 2023年7月13日 15:06:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定