Re: Hello from a super noob!

2017-06-08 Thread Andre Müller
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
>   
>




signature.asc
Description: OpenPGP digital signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Hello from a super noob!

2017-06-08 Thread Rhodri James

On 08/06/17 00:56, CB wrote:

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!


It helps if you describe what is going wrong.  Not just us, either; 
"Teddy Bear Debugging", explaining to a colleague (or indeed a soft toy) 
why your code cannot possibly be wrong, is an excellent way of finding 
bugs.  The number of times I've stopped in the middle of an explanation 
to fix the now glaringly obvious mistake...


Try it and see.  Here's a hint: be very aware of what your indentation 
means.


--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Hello from a super noob!

2017-06-07 Thread Steve D'Aprano
On Thu, 8 Jun 2017 09:56 am, CB wrote:

> Can anyone try to run it?


Yes, you can.

Doctor to patient: "So, what seems to be the problem?"

Patient: "You're the doctor, you tell me."




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Hello from a super noob!

2017-06-07 Thread MRAB

On 2017-06-08 00:56, CB wrote:

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
   


You haven't said what the problem is.

It looks OK, apart from the indentation, which is important to get right 
in Python.


Also, you've defined a function 'main' but not called it, and imported a 
module but not used it, which is pointless.

--
https://mail.python.org/mailman/listinfo/python-list