简体   繁体   English

如何接受负值作为 Python 中的输入?

[英]How can I accept negative values as the inputs in Python?

How can I accept negative values as the inputs?如何接受负值作为输入?

import math
x_coordinates = input("Enter the x coordinates: ")
y_coordinates = input("Enter the y coordinates: ")

x_coordinates = [int(x) for x in x_coordinates]
y_coordinates = [int(x) for x in y_coordinates]

total_x = (x_coordinates[-1] - x_coordinates[0]) ** 2
total_y = (y_coordinates[-1] - y_coordinates[0]) ** 2
total = total_x + total_y
total = math.sqrt(total)
print(total)

Thanks.谢谢。

I assume you're entering input such as 1 -42 14 or something similar.我假设您正在输入诸如1 -42 14或类似的输入。 input() returns a string. input()返回一个字符串。 When you do [int(x) for x in x_coordinates] , you are calling int() on each character in the string.当您执行[int(x) for x in x_coordinates]时,您正在对字符串中的每个字符调用int() So, you should .split() the string on spaces first, then call int() on each member:因此,您应该首先在空格上.split()字符串,然后在每个成员上调用int()

x_coordinates = [int(x) for x in x_coordinates.split()]

Given the above input, this would return鉴于上述输入,这将返回

[1, -42, 14]

input gives you one string, and when you iterate over that, you get each character separately. input给你一个字符串,当你迭代它时,你会分别得到每个字符。 Come up with a separator character, like space, and split the inputs on that:想出一个分隔符,比如空格,然后分割输入:

import math

x_coordinates = input("Enter the x coordinates: ")
y_coordinates = input("Enter the y coordinates: ")

x_coordinates = [int(x) for x in x_coordinates.split(" ")]
y_coordinates = [int(x) for x in y_coordinates.split(" ")]

total_x = (x_coordinates[-1] - x_coordinates[0]) ** 2
total_y = (y_coordinates[-1] - y_coordinates[0]) ** 2
total = total_x + total_y
total = math.sqrt(total)
print(total)

Then:然后:

robert@here:~$ python mcve.py
Enter the x coordinates: 1 2 3
Enter the y coordinates: -1 -2 -3
2.8284271247461903

That said, if you only want to read 2 x and 2 y values anyway, you should probably just use separate variables.也就是说,如果您只想读取 2 x 和 2 y 值,您可能应该只使用单独的变量。

import math

x1 = int(input("Enter x1: "))
x2 = int(input("Enter x2: "))
y1 = int(input("Enter y1: "))
y2 = int(input("Enter y1: "))

total_x = (x2 - x1) ** 2
total_y = (y2 - y1) ** 2
total = total_x + total_y
total = math.sqrt(total)
print(total)

Or at least check how many numbers are entered and print an error.或者至少检查输入了多少数字并打印错误。

import math

x_coordinates = input("Enter the x coordinates: ")
y_coordinates = input("Enter the y coordinates: ")

x_coordinates = [int(x) for x in x_coordinates.split(" ")]
y_coordinates = [int(x) for x in y_coordinates.split(" ")]
if len(x_coordinates) > 2 or len(y_coordinates) > 2:
    print("Need 2 x and y coordinates")
else:
    total_x = (x_coordinates[-1] - x_coordinates[0]) ** 2
    total_y = (y_coordinates[-1] - y_coordinates[0]) ** 2
    total = total_x + total_y
    total = math.sqrt(total)
    print(total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中保存具有负像素值的图像? - How can I save an image with negative pixel values in python? Python:如何用 while 循环替换这个 for 循环? (没有负值) - Python: How can I substitute this for loop with a while loop? (No negative values) 当用户输入负数时如何添加异常 - How can I add exception for when user inputs a negative number 我该如何解释Python中的负值? - How would I account for negative values in Python? 我怎样才能让我的程序拒绝字母输入,只接受数字输入? - How can i get my program to reject letter inputs, and only accept number inputs? 如何在python中使参数接受多个输入 - How to make an argument in python accept multiple inputs 如何在Python中测试负零? - How can I test for negative zero in Python? 如何创建一个矩阵,其中用户输入行和列,然后在 Python 中输入每个 position 的值? - How can I create a matrix in which the user inputs both rows and columns and then enters the values for each position in Python? 如何让循环显示初始输入以及如何接受百分比“%”作为输出 - How can I get the loop to display the initial inputs and how to accept percentages '%' as output 自定义最小值函数不返回负输入,而仅返回零? 我该如何修复我的代码? - Custom minimum function is not returning my negative inputs and just return zero? how can i fix my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM