是否有提示用户输入特定数字的代码?
Is there a code where it prompts the user to enter specific numbers?
我正在练习 Python,我的代码有一些问题。
我正在尝试定义主要功能并提示用户输入三个特定数字。该程序应该找到这三个数字的平均值,并将平均值与输入的数字进行比较,然后计算有多少数字等于平均值。
这是我的代码:
def main():
def introduction():
print("Welcome to My Program!")
print("My name is Alex.")
print("In this program, you will enter three numbers.")
print("The program will find the average of those numbers.")
print("The program will compare those three numbers to the average.")
print("Continue to the next section please....")
n1 = int(input("Please Enter Number 1: "))
n2 = int(input("Please Enter Number 2: "))
n3 = int(input("Please Enter Number 3: "))
print(f'The numbers you entered were: {n1} {n2} {n3}')
def findaverage(n1, n2, n3):
avg = (n1 + n2 + n3)/3
return avg
print(f'The average for those numbers are: {avg:.3f}')
def comparetoavg(a1, a2, a3, avg):
count = 0
number = [a1, a2, a3]
for x in number:
if number > avg:
print(f'This number: {number} is above than the average {avg:.3f}.')
elif number < avg:
print(f'This number: {number} is below than the average {avg:.3f}.')
else:
count += 1
print(f'This number: {number} is equal to the average {avg:.3f}.')
print(f'{count} values are equal to the average.')
main()
- 我的代码无法通过。它只打印了三次“Please Enter Number”并打印了语句。
- 我希望我的代码迭代 10 次。我知道必须使用循环,但我不确定将循环放在哪里。
- 我希望 2 次迭代中的三个值等于平均值,3 次迭代中没有值等于平均值,5 次迭代中有一个值等于平均值。
有人可以帮我吗?
introduction
、findaverage
和 comparetoavg
函数在 main
中定义,但它们从未被调用,因此它们中的代码不会被执行。
您需要调用您的函数,以便它们实际上 运行 并确保它们在同一范围内,以便它们实际上是可调用的
def findaverage(n1, n2, n3):
avg = (n1 + n2 + n3)/3
print(f'The average for those numbers are: {avg:.3f}')
return avg
def comparetoavg(a1, a2, a3, avg):
count = 0
number = [a1, a2, a3]
for x in number:
if x > avg:
print(f'This number: {x} is above than the average {avg:.3f}.')
elif x < avg:
print(f'This number: {x} is below than the average {avg:.3f}.')
else:
count += 1
print(f'This number: {x} is equal to the average {avg:.3f}.')
print(f'{count} values are equal to the average.')
def introduction():
print("Welcome to My Program!")
print("My name is Alex.")
print("In this program, you will enter three numbers.")
print("The program will find the average of those numbers.")
print("The program will compare those three numbers to the average.")
print("Continue to the next section please....")
def main():
introduction()
n1 = int(input("Please Enter Number 1: "))
n2 = int(input("Please Enter Number 2: "))
n3 = int(input("Please Enter Number 3: "))
print(f'The numbers you entered were: {n1} {n2} {n3}')
avg = findaverage(n1, n2, n3)
comparetoavg(n1, n2, n3, avg)
main()
您的代码似乎有几个问题。
除了 main,你没有调用任何函数。主函数里面只有函数定义,没有函数调用。您必须将函数调用放在主函数中,然后调用主函数。充其量,您还将函数定义放在主函数定义之上。
您可以使用 input() 函数来请求用户输入。请参阅下面的示例。
如果要调用函数 10 次,则必须将函数调用放在具有 10 次迭代的 for 循环体中。请参阅下面的示例。
试试这个:
def findaverage(n1, n2, n3):
...
def comparetoavg(a1, a2, a3, avg):
...
def main():
for i in range(0, 10):
n1 = input("Enter number:")
n2 = input("Enter number:")
n3 = input("Enter number:")
avg = findaverage(n1, n2, n3)
comparetoavg(n1, n2, n3, avg)
main()
这只是一个关于如何使用 for 循环、输入函数以及如何正确定义和调用方法的示例。您可能需要根据您的需要进一步调整它。
我正在练习 Python,我的代码有一些问题。 我正在尝试定义主要功能并提示用户输入三个特定数字。该程序应该找到这三个数字的平均值,并将平均值与输入的数字进行比较,然后计算有多少数字等于平均值。
这是我的代码:
def main():
def introduction():
print("Welcome to My Program!")
print("My name is Alex.")
print("In this program, you will enter three numbers.")
print("The program will find the average of those numbers.")
print("The program will compare those three numbers to the average.")
print("Continue to the next section please....")
n1 = int(input("Please Enter Number 1: "))
n2 = int(input("Please Enter Number 2: "))
n3 = int(input("Please Enter Number 3: "))
print(f'The numbers you entered were: {n1} {n2} {n3}')
def findaverage(n1, n2, n3):
avg = (n1 + n2 + n3)/3
return avg
print(f'The average for those numbers are: {avg:.3f}')
def comparetoavg(a1, a2, a3, avg):
count = 0
number = [a1, a2, a3]
for x in number:
if number > avg:
print(f'This number: {number} is above than the average {avg:.3f}.')
elif number < avg:
print(f'This number: {number} is below than the average {avg:.3f}.')
else:
count += 1
print(f'This number: {number} is equal to the average {avg:.3f}.')
print(f'{count} values are equal to the average.')
main()
- 我的代码无法通过。它只打印了三次“Please Enter Number”并打印了语句。
- 我希望我的代码迭代 10 次。我知道必须使用循环,但我不确定将循环放在哪里。
- 我希望 2 次迭代中的三个值等于平均值,3 次迭代中没有值等于平均值,5 次迭代中有一个值等于平均值。 有人可以帮我吗?
introduction
、findaverage
和 comparetoavg
函数在 main
中定义,但它们从未被调用,因此它们中的代码不会被执行。
您需要调用您的函数,以便它们实际上 运行 并确保它们在同一范围内,以便它们实际上是可调用的
def findaverage(n1, n2, n3):
avg = (n1 + n2 + n3)/3
print(f'The average for those numbers are: {avg:.3f}')
return avg
def comparetoavg(a1, a2, a3, avg):
count = 0
number = [a1, a2, a3]
for x in number:
if x > avg:
print(f'This number: {x} is above than the average {avg:.3f}.')
elif x < avg:
print(f'This number: {x} is below than the average {avg:.3f}.')
else:
count += 1
print(f'This number: {x} is equal to the average {avg:.3f}.')
print(f'{count} values are equal to the average.')
def introduction():
print("Welcome to My Program!")
print("My name is Alex.")
print("In this program, you will enter three numbers.")
print("The program will find the average of those numbers.")
print("The program will compare those three numbers to the average.")
print("Continue to the next section please....")
def main():
introduction()
n1 = int(input("Please Enter Number 1: "))
n2 = int(input("Please Enter Number 2: "))
n3 = int(input("Please Enter Number 3: "))
print(f'The numbers you entered were: {n1} {n2} {n3}')
avg = findaverage(n1, n2, n3)
comparetoavg(n1, n2, n3, avg)
main()
您的代码似乎有几个问题。
除了 main,你没有调用任何函数。主函数里面只有函数定义,没有函数调用。您必须将函数调用放在主函数中,然后调用主函数。充其量,您还将函数定义放在主函数定义之上。
您可以使用 input() 函数来请求用户输入。请参阅下面的示例。
如果要调用函数 10 次,则必须将函数调用放在具有 10 次迭代的 for 循环体中。请参阅下面的示例。
试试这个:
def findaverage(n1, n2, n3):
...
def comparetoavg(a1, a2, a3, avg):
...
def main():
for i in range(0, 10):
n1 = input("Enter number:")
n2 = input("Enter number:")
n3 = input("Enter number:")
avg = findaverage(n1, n2, n3)
comparetoavg(n1, n2, n3, avg)
main()
这只是一个关于如何使用 for 循环、输入函数以及如何正确定义和调用方法的示例。您可能需要根据您的需要进一步调整它。