oneminer
oneminer

Reputation: 13

Variables inside functions, can't be used outside the functions

It wont recognize the "text" variables in the if statements. How to change it so when I type in something in the input, it would recognize it and print the thing I want it to print. After that the input message would show again, and you could change the text?

print("TYPING SIMULATOR 1.0")
print("______________________")


def type():
    text = input("type something: ")
    print(text)

type()

if "begin" in text or "start" in text:
    print("Game Started!")
    type()

    if "rob" in text:
        print("you get money")
        type()

Upvotes: 1

Views: 64

Answers (3)

Cow
Cow

Reputation: 3030

If you want the game to run until you enter 'exit' or something similar you could do it like this:

print("TYPING SIMULATOR 1.0")
print("______________________")

while True:
    def type():
        text = input("type something: ")
        return(text)

    text = type()

    if "begin" in text or "start" in text:
        print("Game Started!")
        text = type()

        if "rob" in text:
            print("you get money")
            text = type()
    
    if text == "exit":
        print("Exiting Game!")
        break

Upvotes: 0

user15801675
user15801675

Reputation:

You can always return the value of the function, and check the result

print("TYPING SIMULATOR 1.0")
print("______________________")


def type():
    text = input("type something: ")
    return text 

text=type()

if "begin" in text or "start" in text:
    print("Game Started!")
    text=type()

    if "rob" in text:
        print("you get money")
        text=type()

Upvotes: 3

Julien Sorin
Julien Sorin

Reputation: 855

In my opinion you're trying to get the value given by the input function. So the better way to do that is :

def get_input() -> str:
    return input("type something: ")

text: str = get_input()

Upvotes: 0

Related Questions