May-31-2020, 09:35 AM
Hi, first time on python (only dabbled with VBA previously) and I've done the cliche guessing game (sorry!) Taken me a few days.
I've tried to go further and make it a more robust using functions. I'm finding it really hard to get my head around functions and how to use them with arguments. My code is a little as a result of trial-and-error and I don't quite understand why some of it works. If anyone's willing, could I have some feedback on whether the code is 'neat' or unnecessary or if I've committed any python faux pas? Thank you very much.
I've tried to go further and make it a more robust using functions. I'm finding it really hard to get my head around functions and how to use them with arguments. My code is a little as a result of trial-and-error and I don't quite understand why some of it works. If anyone's willing, could I have some feedback on whether the code is 'neat' or unnecessary or if I've committed any python faux pas? Thank you very much.

# Guessing Game def string_or_integer(guess): while True: if guess in ('quit', 'Quit'): print('Bye bye!') sys.exit() elif not guess in ('quit', 'Quit'): try: return int(guess) except ValueError: try: float(guess) print("Please use whole numbers") guess = input('>>> ') except ValueError: print ("This is not a number. Please enter a number between 1 and 20") guess = input('>>> ') def game(): guess = input('>>> ') guessInt = string_or_integer(guess) if (guessInt == rNum): print('Congratulations! You won!') sys.exit() elif (guessInt > 20): print('Way too high! Guess between ONE and TWENTY') game() elif (guessInt < rNum): print('Try a higher number') game() elif (guessInt > rNum): print('Try a lower number') game() import random import sys rNum = random.randint(1,20) print('Welcome to the Guessing Game!\nGuess a number between 1 and 20\nTo give up, type "quit"') game()