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
  keeping logs for every success fail attempt robertkwild 22 5,389 Jul-19-2024, 03:49 PM
Last Post: robertkwild
  I must be missing something simple? SLFrey 2 1,328 Dec-11-2023, 03:13 PM
Last Post: SLFrey
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 1,764 Oct-18-2023, 11:09 PM
Last Post: Edward_
  How to calculated how many fail in each site(s) in csv files SamLiu 4 2,286 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 3,450 Mar-11-2022, 01:50 PM
Last Post: snippsat
  [SOLVED] Why does regex fail cleaning line? Winfried 5 3,486 Aug-22-2021, 06:59 PM
Last Post: Winfried
  scraping video src fail jacklee26 5 4,794 Jul-11-2021, 09:38 AM
Last Post: snippsat
  Not able to crack a simple visualization – missing something basic – plz guide darpInd 4 3,847 Mar-27-2020, 09:26 AM
Last Post: darpInd
  fail to upgrade pygobject jiapei100 0 3,619 Aug-16-2018, 10:32 AM
Last Post: jiapei100
  Fail to allocate bitmap rsbeesh 7 18,302 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