-3

I found an exercise on the internet and i can't solve it. It asks to make a program that makes the user decide if it wants to use decimals or integers, makes them insert how many numbers they want and stops when the user inserts a string instead of a number

x=("n")
h=str(input("would you like to use decimals or integers "))
z=["decimals","decimal"]
k=["integers","integer"]
while o==True:
    if h not in k and z:
        print ("insert")
        o=False
    
    elif h not in k:
        u0=float(input("insert number "))
        v=u0
        if v==x:
            print ("the sum is ",u, " the product is ", m)
            o=False
        u=u0+j
        m=u0*d
        j=u
        d=m    

I tryed inverting the way the programs work, making it so that you entered numbers that where treated like strings until the program checked that they were not te string to make the program stop and than converting it into a float/integer.

the error message that pops up is:

 u0=int(input("insert number ")), ValueError: invalid literal for int() with base 10: 'n'
Ftel
  • 1
  • 2
  • just collect the value and check it before calling `float()` on it – ti7 May 10 '23 at 17:15
  • You could look into using `try`. – Scott Hunter May 10 '23 at 17:18
  • Use meaningful variable names, and many of the variables are not defined before used. `if h not in k and z` doesn't do what you think either. It's `if (h not in k) and (z)` where in this case `z` is always true because it is a non-empty list. – Mark Tolonen May 10 '23 at 17:32
  • Welcome to Stack Overflow! Please take the [tour]. This code has other problems besides what you mentioned, so it's hard to give a good answer. `o, j, d, u` are not defined for the first loop, and you have [a logic mistake](/q/20002503/4518341) with `if h not in k and z` which means if you enter "integer", the program goes into an infinite loop. Please make a [mre] with minimal code, example input(s), and expected output. For more tips, check out [ask]. – wjandrea May 10 '23 at 17:37
  • Possible duplicate: [How to use user input to end a program?](https://stackoverflow.com/q/16215922/4518341) – wjandrea May 10 '23 at 17:37
  • *"I tryed inverting the way the programs work, making it so that you entered numbers that [were] treated like strings until the program checked that they were not [the] string to make the program stop and than converting it into a float/integer."* -- That sounds like it should have worked... You might want to show what you tried exactly. – wjandrea May 10 '23 at 17:40

1 Answers1

0

What's happening here is that you are trying to convert a non integer n to a integer which throws a ValueError.

What I would recommend is doing:

try:
    u0=int(input("insert number "))
except ValueError:
    print("That is not an integer")

This catches the value error and then tells the user that they entered a non-integer value. You could have it loop back to the beginning of the function if the error happens and say something like "Try again".

Also

  • since o is boolean you can simply write while o instead of while o==True
  • the default datatype for input is a string so h=str(input(...)) is redundant and str() can be removed.
futium
  • 90
  • 1
  • 9