-1

Don't know how to convert strings to integers.

I tried putting int around it and it still didn't work frstn = int(input("Type first number: "))

import math
# frstn means first number
# scndn means second number
op = input("What operator do you want? + - / *: ")
if op == "+":
    frstn = input("Type first number: ")
    scndn = input("Type second number: ")
    print(frstn + scndn)
elif op == "-":
    frstn = input("Type first number: ")
    scndn = input("Type second number: ")
    print(frstn - scndn)
elif op == "/":
    frstn = input("Type first number: ")
    scndn = input("Type second number: ")
    print(frstn / scndn)
elif op == "*":
    frstn = input("Type first number: ")
    scndn = input("Type second number: ")
    print(frstn * scndn)
else:
    print("Invalid operator, reboot calculator.")

Dogo777
  • 7
  • 2
  • 1
    why exactly didn't that work? That should be exactly what you need to do. – Chris Jan 29 '23 at 22:40
  • Why the same 2 input lines multiple times? You could do that just once (with the `int()` that you say doesn't work) before the `if` statements. – John Coleman Jan 29 '23 at 22:42
  • I thought that each elif statement had to have the same out put. – Dogo777 Jan 29 '23 at 22:46
  • Also I don't know how to convert strings to integers. – Dogo777 Jan 29 '23 at 22:48
  • ask user for first and second number before you start all the conditional statements. This way you will not repeat yourself. Regarding your issue, wrapping int() around your input() should work as long as the user enters a number and not letters. (If you want to ensure user enters a number, look into try, except blocks in python) – jjislam Jan 29 '23 at 22:48
  • Do I have to get rid of the "Type first number: " on each elif statement except the initial if statement? – Dogo777 Jan 29 '23 at 22:53
  • 1
    I figured it out. I had to take out all of the "Type first number: " and "Type second number: " on each elif statement and put them above the initial if statement. – Dogo777 Jan 29 '23 at 22:57
  • Welcome to Stack Overflow. "I tried putting int around it and it still didn't work frstn = int(input("Type first number: "))" Well, yes - that's not the only string that needs to be converted to an integer. If there are two input strings, and the goal is to add two integers, then both need to be converted, yes? Anyway, for future questions: please read [ask] and note well that this is **not a discussion forum**. Please try to ask questions explicitly and directly, and without talking about yourself (so, not "I don't know how to do X", but "how can I do X?"). Similarly for titles. – Karl Knechtel Jan 29 '23 at 23:02

1 Answers1

0

You are looking to cast the user input into an int.

firstn = int(input("Type the first number: "))

This will take the user input and turn it into a number that you can now use it for arithmetic operations.

You could probably move your input statements outside the if statements. If you would like to keep the same style then you could think of something along the lines of

firstn = int(input("Type the first number: "))
op = input("What operator do you want? + - / *: ")
secondn = int(input("Type the second number: "))

if op == "+":
   print(firstn + secondn)
....