Python Forum
How to write a response if a user inputs a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a response if a user inputs a string
#4
There are multiple ways to solve this problem. Perhaps the simplest is convert "solution" to a string instead of converting user_answer to an int.

If you need the answer to be an integer you can catch the exception
try:
   correct_answer = solution == int(user_answer)
except ValueError:
   correct_anser = False

if correct_anser:
   # do correct answer stuff
else:
   # do incorrect answer stuff
Reply


Messages In This Thread
RE: How to write a response if a user inputs a string - by deanhystad - Apr-29-2020, 03:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi set string inputs/outputs kwmcgreal 2 2,008 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Problem with accepting multiple string inputs Ryan_Todd 5 2,876 Jan-22-2020, 06:12 PM
Last Post: buran
  How to check if user entered string or integer or float?? prateek3 5 11,281 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  Perminantly saving user inputs + being able to retrieve it ThatOneGuyNoOneKnowsCodes 1 1,877 Oct-23-2019, 11:28 PM
Last Post: DT2000
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 2,459 Sep-15-2019, 04:41 PM
Last Post: snippsat
  Python start from a specific string line and write? searching1 1 2,184 Jun-27-2019, 02:28 PM
Last Post: perfringo
  write image into string format into text file venkat18 2 4,353 Jun-01-2019, 06:46 AM
Last Post: venkat18
  Using Pypdf2 write a string to a pdf file Pedroski55 6 20,015 Apr-11-2019, 11:10 PM
Last Post: snippsat
  user inputs in constructing a dictionary Exsul 3 3,654 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 2,121 Apr-03-2019, 07:59 PM
Last Post: gebel

Forum Jump:

User Panel Messages

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