Struggling to get ValueError to work in python 3 guessing game The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Guessing Game in PythonNumber guessing game for beginnersBeginner - Guessing GamePython number guessing gamePython guessing gameBeginning guessing game in PythonBeginning Python guessing gameGuessing Number(s) Game in PythonNumber guessing game learncpp.com challenge C++Guess-the-number game by a Python beginner

Are my PIs rude or am I just being too sensitive?

Can withdrawing asylum be illegal?

Change bounding box of math glyphs in LuaTeX

Why does the Event Horizon Telescope (EHT) not include telescopes from Africa, Asia or Australia?

ELI5: Why do they say that Israel would have been the fourth country to land a spacecraft on the Moon and why do they call it low cost?

how can a perfect fourth interval be considered either consonant or dissonant?

Sort list of array linked objects by keys and values

How does this infinite series simplify to an integral?

How to delete random line from file using Unix command?

Converting from Markdown-with-biblatex-commands to LaTeX

Road tyres vs "Street" tyres for charity ride on MTB Tandem

What aspect of planet Earth must be changed to prevent the industrial revolution?

Is a pteranodon too powerful as a beast companion for a beast master?

verb not working in beamer even though I use [fragile]

Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?

Was credit for the black hole image misattributed?

What do you call a plan that's an alternative plan in case your initial plan fails?

How to test the equality of two Pearson correlation coefficients computed from the same sample?

Finding the path in a graph from A to B then back to A with a minimum of shared edges

Hopping to infinity along a string of digits

How to prevent selfdestruct from another contract

What information about me do stores get via my credit card?

What are these Gizmos at Izaña Atmospheric Research Center in Spain?

Is above average number of years spent on PhD considered a red flag in future academia or industry positions?



Struggling to get ValueError to work in python 3 guessing game



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Guessing Game in PythonNumber guessing game for beginnersBeginner - Guessing GamePython number guessing gamePython guessing gameBeginning guessing game in PythonBeginning Python guessing gameGuessing Number(s) Game in PythonNumber guessing game learncpp.com challenge C++Guess-the-number game by a Python beginner



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1












$begingroup$


I have followed a tutorial to create a guessing game in python 3 and i thought i could make it better so i created a timed intro with error inputs recognition and it all seemed to be going well.
However, when i came time to ensure when a player inputted anything other than a number(IE exception handling) it just fails out.
The game it self functions just not the non integer in the guessgame



I have tried the following with ValueError:



  • if, else

  • try, except

  • if guess == ValueError

All to no avail, anyway, code is below. Any help to solve this would be much appreciated.



import time

def guessgame():
import random # brings in random generator
number = random.randint(1, 20) # assigns random value between 1, 20
numofguesses = 0 # sets number of guesses to 0
print("Hello what is your name?")
name = input()
print("The number i am thinking of is between 1 and 20", name) # user
defined name
for numofguesses in range(6):# if number of guesses left is less that 6
do the following
print("Take a guess")
guess = input() # assign guess variable to user input
guess = int(guess) # ensures guess is an number

if guess < number:
print("Number is too low!")

if guess > number:
print("Number is too high!")

if guess == number:
break
else:
if ValueError:#THIS IS THE JERK THAT DOESNT WORK!!!!
print("Please enter a number")

if guess == number:
numofguesses = str(numofguesses)
print("Well done", name, "You guessed the number in: ", numofguesses)
gameiniate()

if guess != number:
number = str(number)
print(" Wrong! unlucky, the number was: ", number)

def gameiniate():
print("Would you like to play a game? Yes or No")
game = input().lower()
yeschoice = ("yes", "y")
nochoice = ("no", "n")

if game in yeschoice:
guessgame()
elif game in nochoice: # command to exit script if no or n typed
print("thats too bad")
print("type exit to close")
if input() == "exit":
exit()
else:
print("Please respond with Yes or No")
gameiniate()

time.sleep(4)
gameiniate()








share







New contributor




Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$


















    -1












    $begingroup$


    I have followed a tutorial to create a guessing game in python 3 and i thought i could make it better so i created a timed intro with error inputs recognition and it all seemed to be going well.
    However, when i came time to ensure when a player inputted anything other than a number(IE exception handling) it just fails out.
    The game it self functions just not the non integer in the guessgame



    I have tried the following with ValueError:



    • if, else

    • try, except

    • if guess == ValueError

    All to no avail, anyway, code is below. Any help to solve this would be much appreciated.



    import time

    def guessgame():
    import random # brings in random generator
    number = random.randint(1, 20) # assigns random value between 1, 20
    numofguesses = 0 # sets number of guesses to 0
    print("Hello what is your name?")
    name = input()
    print("The number i am thinking of is between 1 and 20", name) # user
    defined name
    for numofguesses in range(6):# if number of guesses left is less that 6
    do the following
    print("Take a guess")
    guess = input() # assign guess variable to user input
    guess = int(guess) # ensures guess is an number

    if guess < number:
    print("Number is too low!")

    if guess > number:
    print("Number is too high!")

    if guess == number:
    break
    else:
    if ValueError:#THIS IS THE JERK THAT DOESNT WORK!!!!
    print("Please enter a number")

    if guess == number:
    numofguesses = str(numofguesses)
    print("Well done", name, "You guessed the number in: ", numofguesses)
    gameiniate()

    if guess != number:
    number = str(number)
    print(" Wrong! unlucky, the number was: ", number)

    def gameiniate():
    print("Would you like to play a game? Yes or No")
    game = input().lower()
    yeschoice = ("yes", "y")
    nochoice = ("no", "n")

    if game in yeschoice:
    guessgame()
    elif game in nochoice: # command to exit script if no or n typed
    print("thats too bad")
    print("type exit to close")
    if input() == "exit":
    exit()
    else:
    print("Please respond with Yes or No")
    gameiniate()

    time.sleep(4)
    gameiniate()








    share







    New contributor




    Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      -1












      -1








      -1





      $begingroup$


      I have followed a tutorial to create a guessing game in python 3 and i thought i could make it better so i created a timed intro with error inputs recognition and it all seemed to be going well.
      However, when i came time to ensure when a player inputted anything other than a number(IE exception handling) it just fails out.
      The game it self functions just not the non integer in the guessgame



      I have tried the following with ValueError:



      • if, else

      • try, except

      • if guess == ValueError

      All to no avail, anyway, code is below. Any help to solve this would be much appreciated.



      import time

      def guessgame():
      import random # brings in random generator
      number = random.randint(1, 20) # assigns random value between 1, 20
      numofguesses = 0 # sets number of guesses to 0
      print("Hello what is your name?")
      name = input()
      print("The number i am thinking of is between 1 and 20", name) # user
      defined name
      for numofguesses in range(6):# if number of guesses left is less that 6
      do the following
      print("Take a guess")
      guess = input() # assign guess variable to user input
      guess = int(guess) # ensures guess is an number

      if guess < number:
      print("Number is too low!")

      if guess > number:
      print("Number is too high!")

      if guess == number:
      break
      else:
      if ValueError:#THIS IS THE JERK THAT DOESNT WORK!!!!
      print("Please enter a number")

      if guess == number:
      numofguesses = str(numofguesses)
      print("Well done", name, "You guessed the number in: ", numofguesses)
      gameiniate()

      if guess != number:
      number = str(number)
      print(" Wrong! unlucky, the number was: ", number)

      def gameiniate():
      print("Would you like to play a game? Yes or No")
      game = input().lower()
      yeschoice = ("yes", "y")
      nochoice = ("no", "n")

      if game in yeschoice:
      guessgame()
      elif game in nochoice: # command to exit script if no or n typed
      print("thats too bad")
      print("type exit to close")
      if input() == "exit":
      exit()
      else:
      print("Please respond with Yes or No")
      gameiniate()

      time.sleep(4)
      gameiniate()








      share







      New contributor




      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I have followed a tutorial to create a guessing game in python 3 and i thought i could make it better so i created a timed intro with error inputs recognition and it all seemed to be going well.
      However, when i came time to ensure when a player inputted anything other than a number(IE exception handling) it just fails out.
      The game it self functions just not the non integer in the guessgame



      I have tried the following with ValueError:



      • if, else

      • try, except

      • if guess == ValueError

      All to no avail, anyway, code is below. Any help to solve this would be much appreciated.



      import time

      def guessgame():
      import random # brings in random generator
      number = random.randint(1, 20) # assigns random value between 1, 20
      numofguesses = 0 # sets number of guesses to 0
      print("Hello what is your name?")
      name = input()
      print("The number i am thinking of is between 1 and 20", name) # user
      defined name
      for numofguesses in range(6):# if number of guesses left is less that 6
      do the following
      print("Take a guess")
      guess = input() # assign guess variable to user input
      guess = int(guess) # ensures guess is an number

      if guess < number:
      print("Number is too low!")

      if guess > number:
      print("Number is too high!")

      if guess == number:
      break
      else:
      if ValueError:#THIS IS THE JERK THAT DOESNT WORK!!!!
      print("Please enter a number")

      if guess == number:
      numofguesses = str(numofguesses)
      print("Well done", name, "You guessed the number in: ", numofguesses)
      gameiniate()

      if guess != number:
      number = str(number)
      print(" Wrong! unlucky, the number was: ", number)

      def gameiniate():
      print("Would you like to play a game? Yes or No")
      game = input().lower()
      yeschoice = ("yes", "y")
      nochoice = ("no", "n")

      if game in yeschoice:
      guessgame()
      elif game in nochoice: # command to exit script if no or n typed
      print("thats too bad")
      print("type exit to close")
      if input() == "exit":
      exit()
      else:
      print("Please respond with Yes or No")
      gameiniate()

      time.sleep(4)
      gameiniate()






      python-3.x number-guessing-game





      share







      New contributor




      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 55 secs ago









      Nerdalert3000Nerdalert3000

      1




      1




      New contributor




      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Nerdalert3000 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Nerdalert3000 is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217414%2fstruggling-to-get-valueerror-to-work-in-python-3-guessing-game%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Nerdalert3000 is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Nerdalert3000 is a new contributor. Be nice, and check out our Code of Conduct.












          Nerdalert3000 is a new contributor. Be nice, and check out our Code of Conduct.











          Nerdalert3000 is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217414%2fstruggling-to-get-valueerror-to-work-in-python-3-guessing-game%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订