0
How do you print a string up to a certain letter?
So I'm trying to make a calculator but I want to make it where you just have to put 2+3 rather than: 2 + #next input 2 #next input But I'm having trouble defining the first number. So how would I define num1 as everything before the symbol.
6 odpowiedzi
+ 1
Note that this only handles the trivial case you've given, but the concepts can still apply when you want to add more functionality down the line.
First up is pulling out the first number by itself. You can do that by indexing the input on the '+' character:
if users_input.find('+') != -1:
first_int = int(users_input[:users_input.find('+')])
That is, the first integer is everything up to, but not including, the '+', converted to an int. You can index and slice strings just like a regular list.
For something that gets a little closer to what you really want to do, I would use the split() method:
if users_input.find('+') != -1:
int1, int2 = users_input.split('+')
int1 = int(int1)
int2 = int(int2)
...and you can combine those last three lines into:
int1, int2 = (int(x) for x in user_input.split('+'))
Which gives you exactly the same thing.
Hopefully that gives you a pointer in the right direction. Good luck!
0
Well, first, how many digits do u want ur calc to have?
0
However much the user would input.
Let's say the user input's 5+5, well I would need the first number to be defined as 5.
If they enter 56+25, I need the first number to be defined as everything before the '+' symbol, so the 56. Then, obviously, the second number as everything after the + symbol.
0
Here's a program from the Playground for reference:
num1 = float(input("Your number is "))
num2 = input("Another number is ")
result = num1 + num2
print(result)
0
how do I turn my code into a real mobile app
- 1
You would say r"\d\d". Thats the escape character for 2 digits. You would say:
num1 = input()
if num1 != r"\d\d" #if the input isnt a digit
print("Please enter a number.")