Ask coding questions

← Back to all posts
Problem with local variable
Deacon_Cronin (42)

First off, here is the link:
I'm working on this as a project. I'm a high school freshman right now, this is for my CS class. I'm having some trouble as when I call the random mission function normally it works fine and has no problems, but when I call it through the main menu function (check email and go to bar) it breaks. I have no idea why this is happening. Any help would be appreciated

Answered by heyitsmarcus (280) [earned 5 cycles]
View Answer
heyitsmarcus (280)

Here is the repl I made from your code:

So, the problem you're running into is that when difficulty gets passed into your guesser function, difficulty is not of type int. difficulty is actually a string. And the reason for this (took a minute to trace it back) is that when you are passing the difficulty as dif to the random_mission() function, you are passing it directly from input which makes it of type string. But, you need dif to be of type int to pass the if and elif conditions (and to apply any math to it).

if inp == '1':
    #dif is of type string here because it's coming directly from input
    dif = input('CHOOSE DIFFICULTY (1-3): ')

All you have to do to fix this problem is to pass it as an int to the random_mission() function by wrapping it in the int() method.

if inp == '1':
    #now dif will be of type int because it's transformed into an integer
    dif = int(input('CHOOSE DIFFICULTY (1-3): '))

That was all that was needing to be done! Now, when you run it, you can start breaking passwords!

If this solved your problem, please consider upvoting and marking my answer as the accepted answer to close this question. But, I'll still answer any questions you might have.

heyitsmarcus (280)

@Deacon_Cronin You are most welcome! Happy Coding, Deacon!