I'm practicing Python and I am having a few issues with my code. I am trying to define the main function and prompt the user to enter three specific numbers. The program is supposed to find the average of these three numbers and compare the average to the numbers entered then count how many numbers are equal to the average.
This is my code:
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()
- My code will not go through. It only prints "Please Enter Number" three times and prints the statement.
- I want my code to iterate 10 times. I know have to use a loop, but I'm not sure where to place the loop.
- I want 2 of the iterations to have three values equal the average, 3 to have no value equal to the average, and 5 to have one of the values to equal to the average. Can someone please help me?
CodePudding user response:
There seem to be several problems with your code.
You call no functions but the main. The main function has only function definitions in it but no function call. You must place the function calls in the main function and then call the main function. At best, you also place your function definitions above the main function definition.
You can use the input() function to ask for user input. See in below example how.
If you want to call the functions 10 times, you must place their function call in the body of a for loop that has 10 iterations. See below example.
Try this:
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()
This is just an example on how to use for loops, the input function and how to define and call methods properly. You probably need to adapt it further to your needs.
CodePudding user response:
You need to call your functions so they actually run and make sure they are in the same scope so they are actually callable
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()
CodePudding user response:
The functions introduction
, findaverage
and comparetoavg
are defined inside main
, but they are never called so the code in them is not executed.