I have asked the user for input and set out while true,try and expect blocks. I keep getting valueError even though I have set out that integer required should be displayed if I enter a letter.
pass_credit=int(input("please enter your credits at pass:"))
defer_credit=int(input("Please enter your credits at defer:"))
fail_credit=int(input("please enter your credits at fail:"))
I tried everything to correct but I do not understand
CodePudding user response:
You are not including all your code as you describe it. Input section should look like this:
while True:
try:
pass_credit=int(input("please enter your credits at pass:"))
break
except ValueError as e:
print ("Bad integer value entered, try again.")
while True:
try:
defer_credit=int(input("please enter your credits at defer:"))
break
except ValueError as e:
print ("Bad integer value entered, try again.")
while True:
try:
fail_credit=int(input("please enter your credits at fail:"))
break
except ValueError as e:
print ("Bad integer value entered, try again.")
print(f"You entered: {pass_credit=}, {defer_credit=}, {fail_credit=}")
In fact, it would be better to capture the value before conversion to provide a better error message, e.g.:
while True:
try:
entry=input("please enter your credits at pass:")
pass_credit = int(entry)
break
except ValueError as e:
print (f"You entered: {entry}, which is not an integer, try again.")
You stated that you used while
and try..except
. What did you miss? Comment please.
CodePudding user response:
I ran the same code in Python IDLE and its working perfectly fine.
just check if you have some modules which you have imported in the same file. Also Check by running the same code after
if __name__ =='__main__':
pass_credit=int(float(input("please enter your credits at pass:")))
defer_credit=int(float(input("Please enter your credits at defer:")))
fail_credit=int(float(input("please enter your credits at fail:")))
What is the input numbers that you are giving , is it like 10K for 10000. As 10k by default will be considered string. Since your error mentions of 'k' or does the input have decimal numbers?