-2
while True:
    printing("Welcome to The Cheapest Dealer Ship\n", 0.025)
    printing("What car would you like\n")
    printing(
        """
(1) Standard $50
(2) SUV      $60
(3) Minivan  $80
""", 0.025)

    ans = input(">>")

    try:
      int(ans)
    except:
      pass
    

    if ans == 1:
        Total += 50
        Car = "Standard"
        Car_cost = 50
        break
    elif ans == 2:
        Total += 60
        Car = "SUV"
        Car_cost = 60
        break
    elif ans == 3:
        Total += 80
        Car = "Minivan"
        Car_cost = 80
        break
    else:
        printing("Please chose a valid option")
        time.sleep(1)
        replit.clear()

So basically this is a school code excersize but I can't quite figure why it will return with an else statement even if you answer 1,2 or 3. Not sure why int isn't working or what I'm doing wrong.

Joel
  • 1
  • 1
  • 1
    `int(ans)` returns a new value that you have to store. It is not an in place operation, just change `int(ans)` to `ans = int(ans)` – mwo Feb 16 '22 at 14:07
  • 1
    int(ans) does return an integer, but you're not storing it! Replace the line by `ans = int(ans)` – Jenny Feb 16 '22 at 14:08
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/q/20449427/6045800) – Tomerikoo Feb 16 '22 at 15:57

1 Answers1

0

Either change your this line

int(ans)

To:-

ans = int(ans)

Or, you can directly take an integer as a input.

Change these lines of code

ans = input(">>")
try:
    int(ans)
except:
    pass

To this,

while True:
    ans = (input(">>"))
    if ans.isnumric():
        while True:
            if ans=="3" or ans=="2" or ans=="1"
                break
            else:
                print("Please select any one (1\2\3)")
        break
    else:
        print("Please enter only numbers.")
    ans = int(ans)

After this you will have no need to use try and except statements and you will have less chances of getting errors.

CodeWithYash
  • 223
  • 1
  • 15