New course! Every coder should learn Generative AI!
Try a free lesson+ 2
To appending result to string in print. so..
Result = num1 + num2
print("The ans" + Result) gives Typeerror because result is float adding to string....
Alternatively
You can write this without converting
result =num1+num2
print("the answer is ", result)
Edit: else writeDeepshikha Chhaperia
result =num1+num2
print("The answer is")
print(result)
+ 2
Cos you can only join same type objects,( i.e str + str), before using the + operator you have to convert the int to a string.
+ 1
You can try the following code,no need to convert float into str
elif user_input == "add":
num1 = float(input("Enter a number: "))
num2 = float(input("Enter another number: "))
result = num1 + num2
print("The answer is " ,result)
In your code in print statement you are using "+" operation which is used to add two string...that is why result is converted into string otherwise I will raise error
0
while True:
print("enter 'add' to add two values")
print("enter 'subtract' to subtract two values")
print("enter 'quit' to end")
user_input=input(":")
if user_input == "quit":
break
elif user_input == "add":
num1=float(input("enter a number"))
num2=float(input("enter a number"))
result= num1+num2
print(result)
elif user_input == "subtract":
num1=float(input("enter a number"))
num2=float(input("enter a number"))
result= num1-num2
print(result)
else :
print("not known")
#Deepshikha Chhaperia
#just an example adding this
if num1>=1 :
print(num1,num2)
# indentation means adding spaces before statements
#observe spaces how added.. All if else statement comes under while except last if block...
0
Deepshikha Chhaperia
Use are using while True: and breaking when input is quit so to stop loop you should enter quit.(see line 7)
Example input:
add
4
5
quit
Ex2:
subtract
4
3
add
2
3
quit
0
Because if you will try to print float with it, it will generate type error so to remove that, we have to always pass string variables in print else typecast it before printing. So here you can do this
print('The answer is', str(num1+num2)) or u can replace num1+num2 by result
0
Deepshikha Chhaperia
[If you show the code with which you are trying, then we try to catch errors..]
And
Those 6 Statements [ + : +output 5.0] are which specified only..
First 3 for information of to enter add or subtract or quit
by print function. Next to for input num1 value, then num2 value by input function.
And next is answer..
In
input("enter number") is prompt type statement ask for input a number by displaying information provided as argument to input,.. Here in example "enter number" is displayed on output console..
If you asking anything else or need any changes in Program then leave a comment...
0
sacma soru gercekten manasizsiniz
0
Deepshikha Chhaperia if u r just getting started don't go to advance level now, programing takes tims
0
No need to convert, without str it will work great.
First of all you are messed up with indentation.
Second you forgot to put colon to your last else block.