This is my code for my Computer Science class. I added a feature to make sure the user does not enter a non-int/float. I am trying to access what the users entry to repeat it so it goes like this:
Enter your first number:
> not_a_valid_float_or_int
"not_a_valid_float_or_int" is not a valid number, try again.
So basically I am trying to replace "What you have entered" with what the user had entered.
print("Hello!\n")
while True:
try:
firstnumber = float(input("Enter your first number:\n"))
except ValueError:
print("\nWhat you have entered is not a valid number, try again.")
else:
break
print()
while True:
try:
secondnumber = float(input("Enter your second number:\n"))
except ValueError:
print("\nWhat you have entered is not a valid number, try again.")
else:
break
print("\nThe first number is:", str(firstnumber).rstrip("0").rstrip(".") , "\nThe second number is:", str(secondnumber).rstrip("0").rstrip("."), "\nThe sum is:", str(firstnumber + secondnumber).rstrip("0").rstrip("."), "\nThe product is:", str(firstnumber * secondnumber).rstrip("0").rstrip("."))
Thanks a lot!
David
P.S. please know that I am very knew to the coding scene.
try/exceptblocks, thentryto usefloat(), and if an error is caught then you can use that string in the error printing.