im learning programming on my own and this code is not printing "correct"

Viewed 32

I want the user to put two numbers and put the answer and the software to print correct if he had the right answer but it keeps printing wrong

x = input("num1: ")
y = input("num2: ")
z = input ("num1 + num2: ")

results = float(x) + float(y)
if z == results:
    print("correct")
else :
    print("wrong")
1 Answers

input returns str type.
float(x) and float(y) are for converting str to float.
So you need same thing for z.

Comparing str and float always results False(in this example, '3.0' != 3.0).

x = input("num1: ")
y = input("num2: ")
z = float(input("num1 + num2: "))

results = float(x) + float(y)
if z == results:
    print("correct")
else:
    print("wrong")

output:

num1: 1
num2: 2
num1 + num2: 3
correct
Related