Hello from a super noob!
Andre Müller
gbs.deadeye at gmail.com
Thu Jun 8 08:39:53 EDT 2017
Hello,
you can refactor your code a little bit and learn more about exceptions:
def get_numbers():
first = None
second = None
while True:
try:
if first is None:
first = int(input('Enter your first number: '))
if second is None:
second = int(input('Enter your second number: '))
except ValueError:
print('You have to enter a number')
continue
else:
return first, second
Am 08.06.2017 um 01:56 schrieb CB:
> Hi everyone,
> I am taking a python class and I'm stuck in an exercise.
>
> what am i doing wrong? Can anyone try to run it? Thanks so much!
>
> #Description:Input validation and while loops.
>
>
> import random
> def main(): #main function need in all programs for automated testing
>
>
> #your program goes here
>
> print()
>
>
>
>
> print("This program will help us practice input validation and while loops.")
> print("The user will be asked to enter two numbers which will both be validated. ")
> print("The sum of the numbers will then be displayed in a complex print statement ")
> print("and the user will be asked if they would like to run the program again."
> )
> print()
> print()
>
> while True:
> FirstNumber = input ("Please enter the first number: ")
> if FirstNumber.isdigit ():
> FirstNumber = int(FirstNumber)
> break
> else:
> print ("Invalid response. Please enter a whole number. " )
>
> while True:
>
> SecondNumber = input ("Please enter the second number: " )
> if SecondNumber.isdigit():
> SecondNumber= int(SecondNumber)
>
> break
> else:
> print("Invalid response. Please enter a whole number." )
>
> print()
> print (str(FirstNumber) + " + " + str(SecondNumber)+ " = " + str(FirstNumber + SecondNumber))
> print()
>
> while True:
>
> ans= input('Would you like to run the program again (Y/N) : ')
> if ans== 'Y' or ans== 'N':
> break
>
> else:
> print(" lnvalid response. Please answer with 'Y' or 'N' ")
>
> if ans== 'N':
> break
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20170608/1511859a/attachment.sig>
More information about the Python-list
mailing list