print('This program calculates the average numbers entered by the user')
N = int(input('How many numbers do you have?: '))
y = 0
for i in range(N):
x = float(input('Please enter you number: '))
y = x + y
print(f'The average of your numbers is {y/N:.1f}')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print('This progrsm calculates the average numbers entered by the user')
N = int(input('How many numbers do you have?: '))
y = 0
keepGoing = True
N = 0
while keepGoing:
x = float(input('Please enter you number: '))
y = x + y
N = N+1
keepGoing = input('do you have more numbers? [y/n]').lower() == 'y'
print(f'The average of your numbers is {y/N:.1f}')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print('This progrsm calculates the average numbers entered by the user')
N = 0
y = 0
while True:
x = float(input('Please enter number(enter -1 to end program): '))
if x >= 0:
y = x + y
N = N+1
else:
break
print(f'The average of your numbers is {y/N:.1f}')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print('This progrsm calculates the average numbers entered by the user')
N = 0
y = 0
while True:
x = float(input('Please enter number(enter -1 to end program): '))
if x >= 0:
y = x + y
N = N+1
else:
break
print(f'The average of your numbers is {y/N:.1f}')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
print('This progrsm calculates the average numbers entered by the user')
N = 0
y = 0
while True:
try:
x = float(input('Please enter number(enter -1 to end program): '))
y = x + y
N = N+1
except:
break
print(f'The average of your numbers is {y/N:.1f}')
Comments
Displaying 1 of 1 comments ( View all | Add Comment )
FravelTooPointOh
cool beginner stuff, but here's some tips that you can use
the first program is good but instead of doing "y = y + x" you can do "y += x" and it'll make your code look a lot more professional. It doesn't change anything but it looks nice yknow?
As for the second program, you do N = 0 making it redundant to ask the user for the number of inputs they want. I'm assuming based on you incrementing the N variable you wanted to make it so you'd ask the user if they wanted to add anymore numbers after the loop is done. Instead of using a while loop, you could use something called a for loop where you don't need to use a boolean and you don't need to worry about writing a line to increment your variable. After the for loop you could include a while loop with the keep going thing.
Third program is solid, but instead of doing N = N + 1 you could do N++ and it does the same thing. The fourth one seems to be the same thing so I won't comment on it
I think it's really cool that you're trying new stuff out like with the try except statement in your 5th program, but it doesn't actually end the program when you type in -1 because it's only checking for errors that can occur (ie: the user inputs "Hello" instead of an actual number). It's still good to use them though, just keep that in mind that try exception statements are used for different things.
Thanks for the tips!
This is mostly super simplified stuff that's for beginners. You're right, I could've added an if statement or something and I didnt realize i duplicated my codes woops ;~;
by Kearo Kai; ; Report
oh yeah no worries, good luck on any future projects!
by FravelTooPointOh; ; Report