此代码旨在为用户提供平均值,中位数,模式或退出的选项,但是我无法选择选项1 - 4以便在选择该选项时将会发生什么。
ans1=ans1
ans2=ans2
# define functions
def average(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10):
"""This function adds two numbers"""
return total== num1, + num2, + num3, + num4, + num5, + num6, + num7, + num8, + num9, + num10 == ans1
total/ans1
def median(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10):
"""This function subtracts two numbers"""
return total == num1 - num2 - num3 - num4 - num5 - num6 - num7 - num8 - num9 - num10 == ans2
total/ans2
def mode(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10):
"""This function multiplies two numbers"""
return num1 * num2, num3, num4, num5, num6, num7, num8, num9, num10
def quit():
quit()
# This gives the user the options in a menu format
print("Select operation.")
print("1.average")
print("2.median")
print("3.Mode")
print("4.quit")
#this is where the user enters the number for what the user wants done
choice = input("Enter choice 1,2,3,4: ")
#################################################################
#this then asks the user for the 10 numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("enter third number: "))
num4 = int(input("enter fourth number: "))
num5 = int(input("enter fith number: "))
num6 = int(input("enter sixth number: "))
num7 = int(input("enter seventh number: "))
num8 = int(input("enter eights number: "))
num9 = int(input("enter ninth number: "))
num10 = int(input("enter tenth number: "))
print(num1)
print(num2)
print(num3)
print(num4)
print(num5)
print(num6)
print(num7)
print(num8)
print(num9)
print(num10)
if choice == '1':
print (ans1, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10)
def adding (num1, num2, num3, num4, num5, num6, num7, num8, num9, num10):
s = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)/10
print("this is what they add up to")
print(s)
return s
elif choice == '2':
print(ans2(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10))
elif choice == '3':
print(num1,"*", num2, num3, num4, num5, num6, num7, num8, num9, num10,"=", multiply(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10))
elif choice == '4':
quit()
答案 0 :(得分:2)
因为我不认为这是家庭作业,但只是有人试图学习一些Python,我将部分展示如何做到这一点。
首先,我们将以易于理解的函数分离程序,其基础将是def showMenu():
while True:
print("Select operation.")
print("1.average")
print("2.median")
print("3.Mode")
print("4.quit")
userInput = input()
函数:
True
请注意,此循环的条件始终为def showMenu():
while True:
...
userInput = input()
if userInput == "1" or userInput == "2" or userInput == "3":
# Do something
elif userInput == "4":
exit()
else:
print("Invalid input, try again")
,因此无法转义。要添加一些逻辑,我们必须解析用户输入:
4
现在,如果用户输入if userInput == "1":
showAverageMenu()
elif userInput == "2":
showMedianMenu()
elif userInput == "3":
showModeMenu()
elif userInput == "4":
exit()
else:
print("Invalid input, try again")
,程序可以退出循环,如果他们输入任何其他无效值,程序只会再次显示菜单并向用户询问输入值。现在,当输入为1,2或3时,让我们填写案例的部分。我们编辑if语句以将这些案例分开并创建一个新函数来处理它们:
showAverageMenu()
好的,让我们下一步是创建getInput()
功能。但是,我们首先要讨论我们处理用户输入的方式。你这样做的方法是手动询问十个不同的输入值,并将它们存储在十个不同的变量名下。如果我们希望用户输入15个值,或30或100,该怎么办?这需要大量的重复编码。幸运的是,还有更好的方法:我们将用户输入存储在列表中。此列表可以与用户想要的一样大。因为我们要求用户输入数字的方式在三个不同的操作中是相同的,所以我们将创建一个我们每次都可以调用的函数。此函数将被称为intValue = int(stringValue)
。这个函数的逻辑看起来像这样:
虽然没有完成:
我们已经看过如何询问用户输入,但是,与上次只有数字1到4有效的时间不同,现在所有数字都是有效输入。此外,上次我们将输入作为字符串处理,但为了计算平均值,我们将需要整数(或浮点)值。要将字符串转换为整数,我们可以强制转换输入:ValueError
。但是,如果用户没有输入数字而是输入一些非数字字符,则此功能提升 a def getInput():
inputList = []
print("Please enter an integer")
while some_condition:
userInput = input()
try:
intValue = int(userInput)
inputList.append(intValue)
except ValueError:
print("'{}' is not an integer".format(userInput))
。为了解决这个问题,我们必须捕获错误。
足够的话,让我们展示一些代码:
ValueError
在这里,您可以看到我们尝试将用户输入转换为整数,但如果失败,则会捕获inputList
,并及时提醒用户他们应输入数。另请注意,此函数以名为def getInput():
userInput = None
inputList = []
print("Please enter an integer")
while userInput != "done" or len(inputList) == 0:
userInput = input()
...
print("'{}' is not an integer".format(userInput))
print("Please enter an integer")
if len(inputList) > 0:
print("Or type 'done' if you are finished")
return inputList
的空列表开头,如果转换未失败,则会填充整数值。最后,请注意while循环的条件,因为它已设置为一些未定义的变量,但是所需的行为是什么?首先,输入列表必须包含至少一个元素,其次用户必须发信号通知它们已完成输入值。在代码中,这可以这样做:
userInput
现在我们首先将None
设置为showAverageMenu()
,以便我们可以在条件中使用该变量。如果输入列表为空,我们也继续循环。最后,如果输入列表确实包含数字,我们会告诉用户他们可以输入' done'退出循环,这反映在循环的条件中。如果用户完成,我们将返回列表。
现在我们已经创建了一个从用户检索输入的通用解决方案,我们必须定义实现这些操作的函数。我们来看看def showAverageMenu():
print("***Averaging***")
inputList = getInput()
avg = sum(inputList) / len(inputList))
print("The Average of")
print(inputList)
print("is: {}\n".format(avg)
:
{{1}}
如您所见,解决方案非常简单。我们所要做的就是计算平均值并打印出来。为了计算平均值,我们可以使用内置方法计算列表的总和,并将其除以列表的长度。
现在其他两种方法有点困难,但留给读者练习;)
答案 1 :(得分:0)
我为你修复了你代码的几个部分,然后发表评论来解释我的所作所为。在这段代码中有几件事需要修复,我并没有解决所有这些问题。我解释了我做了什么,所以你应该能够通过并修复它们,但如果你有任何问题留下评论,我会尽可能多地解释。正如许多人上面所说,你应该通过python教程。 Codecademy has a relatively good one.
#You should declare these first to ensure that you have access to them throughout the program
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("enter third number: "))
num4 = int(input("enter fourth number: "))
num5 = int(input("enter fith number: "))
num6 = int(input("enter sixth number: "))
num7 = int(input("enter seventh number: "))
num8 = int(input("enter eights number: "))
num9 = int(input("enter ninth number: "))
num10 = int(input("enter tenth number: "))
print(num1)
print(num2)
print(num3)
print(num4)
print(num5)
print(num6)
print(num7)
print(num8)
print(num9)
print(num10)
#It looks like you are trying to use the variables above, so you don't need them as parameters.
#If you use them as parameters, the function will get confused and use the wrong ones.
def average():
"""This function adds two numbers"""
#Set total before you try to return it.
#When you create a variable, it's just 1 equal sign, it's 2 when you are checking if something is equal
#Also, never use 2 equal signs such as total = x == y
#When you are adding something, you don't need commas.
total = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10
#The return statement should be the last thing in a function
return total
def median():
"""This function subtracts two numbers"""
total = (num1 - num2 - num3 - num4 - num5 - num6 - num7 - num8 - num9 - num10)/2
return total
#I'll let you take it from here.
def mode(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10):
"""This function multiplies two numbers"""
return num1 * num2, num3, num4, num5, num6, num7, num8, num9, num10
def quit():
quit()
# This gives the user the options in a menu format
print("Select operation.")
print("1.average")
print("2.median")
print("3.Mode")
print("4.quit")
#this is where the user enters the number for what the user wants done
choice = input("Enter choice 1,2,3,4: ")
if choice == '1':
print (ans1, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10)
def adding (num1, num2, num3, num4, num5, num6, num7, num8, num9, num10):
s = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)/10
print("this is what they add up to")
print(s)
return s
#The first if statement should be an if statement, not an elif statement. Since this is a new function, it's a new if statement.
if choice == '2':
print(ans2(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10))
elif choice == '3':
print(num1,"*", num2, num3, num4, num5, num6, num7, num8, num9, num10,"=", multiply(num1, num2, num3, num4, num5, num6, num7, num8, num9, num10))
elif choice == '4':
quit()