-1

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

  • 1
    please use the [edit] button to clarify your question rather than adding code as a comment – Michael Delgado Nov 10 '22 at 04:27
  • line 2, in pass_credit=int(input("please enter your credits at pass:")) ValueError: invalid literal for int() with base 10: 'k' – user20463955 Nov 10 '22 at 04:34
  • 1
    Using the `int` function does not protect or warn the user about an invalid character. It works from the inside out: the `input` gets whatever character the user types, then that string is passed to the `int` function, where you get the value error. You want to use a 'try..catch` block around each input to catch the value error, and report the error to the user. You'll probably want a while loop around all that to repeat the input until a valid value is given. – RufusVS Nov 10 '22 at 04:45
  • 1
    Your sample code doesn't match what you are describing in your question. – RufusVS Nov 10 '22 at 04:46
  • python isn't mysterious. if you're implementing the code as RufusVS has implemented it, you can't get this error. so you're not implementing it this way. feel free to actually include all your code in your question... we can't guess how you've written your code :) check out the guide to [ask] and try to provide a *complete* [mre] in your question. – Michael Delgado Nov 10 '22 at 06:04

2 Answers2

1

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.

RufusVS
  • 4,008
  • 3
  • 29
  • 40
0

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?

  • (Tangentially, code you put in `if __name__ == '__main__’:` should be trivial; the purpose of this boilerplate is to allow you to `import` the code, which you will not want to do anyway if the logic you need is not available via `import`. See also https://stackoverflow.com/a/69778466/874188) – tripleee Nov 10 '22 at 06:28