0

I am facing an issue with the Python code below. It is supposed to execute what is in the flow chart below, but when the value is within range and wrong, the text 'Wrong! Try again. You can exit anytime by pressing Ctrl + C' never gets printed. The text 'Out of range' is being printed instead.

Flow chart

Below is the code I am using:

import random
from ast import literal_eval

x = random.randint(1,10)
y = 0

def f1():
    if y not in range(1,11):
        print('Out of range')
    else:
        print('Wrong! Try again. You can exit anytime by pressing  Ctrl + C')
def f2():
    print('Well done!')
def f3(x,y):
    while x != y:
        try:
            y = literal_eval(input('Guess a number from 1 to 10:\n'))
            if x != y:
                f1()
                continue
            else:
                f2()
                break
        except ValueError:
            print('This is not a number')
        except SyntaxError:
            print('This is not a number')
        except KeyboardInterrupt:
            break
f3(x,y)

Any help or explanation would be very appreciated. Thank you in advance!

User
  • 21
  • 3
  • 3
    The problem is that your global variable `y` is different to the local variable named `y` in your function `f3`. – kaya3 Nov 14 '21 at 11:09
  • Please also read https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers . Your approach is much more complicated than necessary. – Karl Knechtel Nov 14 '21 at 11:10
  • Don't use `ast.literal_eval` to check for a valid integer. It can support strings, tuples and a few other data strctures. Try `'a'` for your input and see. Simply do `try: int(input())` – Reti43 Nov 14 '21 at 11:11
  • You do not need or want to use global variables to solve this problem. You should instead make sure that you understand how arguments, parameters and return values work. It would be better if you simply follow a tutorial start to finish. – Karl Knechtel Nov 14 '21 at 11:12
  • in line 8: did you mean `if x not in range(1,11):`? – celphato Nov 14 '21 at 11:12
  • @kaya3 how to fix this please? I cannot get it right while using functions. – User Nov 14 '21 at 11:16
  • @celphato I meant if y not in range(1,11): because y is the value guessed by the user. If is is not in range the text 'Not in range' should be printed – User Nov 14 '21 at 11:18
  • ah, my bad sorry :O you're right – celphato Nov 14 '21 at 11:22
  • Guys, any advice on how to fix this please? I am new to Python and still facing issues with functions. Thank you for your help and patience! – User Nov 14 '21 at 11:25
  • if you want to use global variables in your function, use `global y` at the very start of your function. however even this breaks because you have `y` as a parameter in `f3`. try not to use global variables in your function and use only constants (`UPPERCASE_VARIABLES`) or parameters (passed into your function), or define new local variables. if you really have to, use `global y` at the very start of your function and be careful not to use `y` as a parameter or confuse it as a local variable in any other function. sorry for the blunder earlier, and I hope this isn't exactly an answer. – celphato Nov 14 '21 at 13:11

0 Answers0