gpt4 book ai didi

python - 如何将用户的号码添加到列表中

转载 作者:行者123 更新时间:2023-11-30 23:05:12 24 4
gpt4 key购买 nike

我一直在尝试编写一个均值、中位数和众数计算器。我有两个问题。 1)如何将用户输入的数字添加到空列表中。如果我只是输入数字,就会出现以下错误:

userNumbers = int(raw_input("Enter the digits here: "))
ValueError: invalid literal for int() with base 10: '2, 4, 6, 8'

我很想知道如何避免这种情况。其次,我使用的是 Python 2.7.10,很快就会升级到 3.4。我知道在 3.4 上有一个名为 stats 的模块,带有中值函数,但它在 2.7.10 上不起作用。出于好奇,如果没有该函数,我将如何找到中位数。谢谢大家!

from collections import Counter


def numberPlacement():
print("Welcome to the PyCalculator, please enter a series of digits!")
userNumbers = int(raw_input("Enter the digits here: "))
userNumbers = []


def userChoice():
while True:
print("Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!)")
userAnswer = raw_input("Enter your choice here: ")
if userAnswer == "Mean":
mean = sum(userNumbers) / float(len(userNumbers))
return mean
elif userAnswer == "Mode":
mode = Counter(userNumbers)
return mode
continue

print numberPlacement()
print userChoice()

最佳答案

您收到的错误是因为您试图将无效字符转换为 int。

您的输入是:1, 2, 3, 4所以这些逗号就是导致该错误的原因。

要纠正这个问题,只需删除您拥有的 int() 转换,通过附加 .split(',') 使您的输入成为一个列表。您的输入。该分割的作用是通过逗号分割将字符串转换为列表。这样您将获得一个由字符串表示的数字列表。

因此,由于您正在做数学,因此需要确保您实际上使用的是列表中数据的数字表示形式,而不是字符串表示形式。您可以通过使用 Python 的 map 来完成此任务,这是一个简洁的单行解决方案。方法( documentation ),它将把所有条目转换为 int。

因此,您的用户输入可以简单地如下所示:

Python 3:

userNumbers = list(map(int, input("Enter the digits here: ").split(',')))

Python 2:

userNumbers = map(int, raw_input("Enter the digits here: ").split(','))

您的代码中还有另一个问题。在您的 userChoice 方法中,您正在引用 userNumbers 。我认为这是对 userNumbers 的引用里面numberPlacement方法。你不能这样做。

我建议做的是,因为您似乎想分别调用这两个方法,所以让您的 userChoice 方法接受一个参数,该参数是一个列表,称为 userNumber。简单地说:

def userChoice(userNumbers):
# the rest of your code

现在,记住这一点非常重要:userNumbers在你的userChoice方法与 userNumbers 不一样在你的numberPlacement方法。要了解原因,这是范围界定的一部分。您可以阅读 here .

所以,当你现在调用你的方法时,你只需要做这样的事情:

number_placements = numberPlacement()
print(number_placements)
print(userChoice(number_placements))

当你把它们放在一起时,你的代码现在看起来像这样:

(Python 2)

from collections import Counter


def numberPlacement():
print("Welcome to the PyCalculator, please enter a series of digits!")
userNumbers = map(int, raw_input("Enter the digits here: ").split(','))
return userNumbers


def userChoice(userNumbers):
while True:
print("Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!)")
userAnswer = raw_input("Enter your choice here: ")
if userAnswer == "Mean":
mean = sum(userNumbers) / float(len(userNumbers))
return mean
elif userAnswer == "Mode":
mode = Counter(userNumbers)
return mode
continue


r = numberPlacement()
print(userChoice(r))

演示:

Welcome to the PyCalculator, please enter a series of digits!
Enter the digits here: 1,2,3,4
Awesome, now that we haave your numbers please choose an operation (Mean, Median, or Mode!)
Enter your choice here: Mode
Counter({1: 1, 2: 1, 3: 1, 4: 1})

关于python - 如何将用户的号码添加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33292644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com