Python Forum
simple if fail, what am I missing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple if fail, what am I missing?
#1
solution = 7
answer = input ("What is 5 + 2?")
if answer == solution:
   print("You are correct!")
   print (f"Your answer was {answer}")
   print (f"and the solution is {solution}")
else:
   print("You are not correct")
   print (f"Your answer was {answer}")
   print (f"and the solution is {solution}")
You would think that entering a 7 for the answer would get a print of 'You are Correct!,
but it always comes up like this:
Output:
$ python ex4-1.py What is 5 + 2?7 You are not correct Your answer was 7 and the solution is 7
What am I missing?
buran write Nov-13-2024, 06:39 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
You need to convert the input to an int or make 7 a string

Output:
What is 5 + 2?: 7 solution is type <class 'int'> answer is type <class 'str'>
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
I tried that and got
Error:
solution is type <class 'int'> ^^^^^ SyntaxError: invalid syntax
but did this
solution = str(7)
and it worked.
buran write Nov-13-2024, 06:39 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,936 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
  keeping logs for every success fail attempt robertkwild 22 8,833 Jul-19-2024, 03:49 PM
Last Post: robertkwild
  I must be missing something simple? SLFrey 2 2,075 Dec-11-2023, 03:13 PM
Last Post: SLFrey
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 3,036 Oct-18-2023, 11:09 PM
Last Post: Edward_
  How to calculated how many fail in each site(s) in csv files SamLiu 4 3,046 Sep-26-2022, 06:28 AM
Last Post: SamLiu
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 4,436 Mar-11-2022, 01:50 PM
Last Post: snippsat
  scraping video src fail jacklee26 5 5,871 Jul-11-2021, 09:38 AM
Last Post: snippsat
  Not able to crack a simple visualization – missing something basic – plz guide darpInd 4 4,875 Mar-27-2020, 09:26 AM
Last Post: darpInd
  fail to upgrade pygobject jiapei100 0 4,254 Aug-16-2018, 10:32 AM
Last Post: jiapei100
  Fail to allocate bitmap rsbeesh 7 20,241 Dec-13-2017, 01:36 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020