Home > Software engineering >  Started Python today, and came to a problem i couldnt really solve [closed]
Started Python today, and came to a problem i couldnt really solve [closed]

Time:09-16

I wanted to dive into Python and I came to a problem when I wanted to make a very primitive calculator.

It asks you to type 2 numbers, and then it would display informations like number1 number 2 etc.

I was at the point where I was typing in to display a short text and then the amount of the two numbers like print("amount:" number1 number2) and that one wasn't right, it was just putting the two numbers next to each other, not what they equal.

I don't know how to display it somehow like this (for example):

The two number equals: 100

CodePudding user response:

You probably did:

a = input('number 1')
b = input('number 2')
print(a   b)

Here a and b will be str (strings are words or sentences). So if you do it'll be like:

a = 'a_word'
b = 'another_word'
print('a_word' 'another_word')
>>> a_wordanother_word

What you want is to make the word into a number. You can do this by 'casting it' to an integer (a whole number) or a float (a number with a decimal):

a = int(input('number_1'))  # example: a = 5
b = float(input('number_2'))    # example: b = 1.2345
print(a   b)
>>> 6.2345

CodePudding user response:

Instead of number1 = input(), you have to make it number1 = int(input()) (you can also use float() or whatever) , otherwise your number is going to be a string.

CodePudding user response:

I'm not sure of what you are trying to do but i think it should work for your case :

a = 10

b = 5

print(f'A B = {a b}')

  • Related