Jump to content

so in bat files you can do

x

*code go here*

go to x

and it will loop back to there, i wish to have this in my code for python 3 where if you have an option that the computer doesnt understnad then it will continue that section till it words, i tried to use while commands but i have a variable inside that cant be changed, so thats my major issue.

UwU

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/
Share on other sites

Link to post
Share on other sites

Just now, Spoiled_Kitten said:

so the variable whenever i use the while statement, as soon as an answer my program can understand goes through, it disappears

If I understand correctly what you actually mean, you need to define the variable before the while-loop, like e.g.:

answer = ""
counter = 0

while True:
	#Insert code to do some useful stuff here
    counter += 1
    if(counter == 5):
    	answer = 42
        break

#answer was defined before the loop, so it doesn't "disappear", ie. go out of scope, after the loop exits.
print(f"The answer to the meaning of life is: {answer}")

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13961247
Share on other sites

Link to post
Share on other sites

4 hours ago, Spoiled_Kitten said:

i get an error

.....aaaand you never mention the error you get. Wow, that's very fucking useful.

 

I assume it's that print-clause where I use an f-string. Well, that's easy to fix: just replace the print-clause with something else! The print-clause wasn't the point there, the point was demonstrating how declaring the variable before the loop fixes the issue.

 

Also, quote people if you reply to them. I didn't know you had replied until just now, because you didn't quote and thus I never got a notification about it.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13961642
Share on other sites

Link to post
Share on other sites

Aren't you just talking about using functions? You can use your variable as input for your function and also set your variable to the output of said function.

 

def myCoolFunction(input):
    functionOutput = input + 1
    return functionOutput

myCoolVariable = 1
print("myCoolVariable is: " + str(myCoolVariable))
myCoolVariable = myCoolFunction(myCoolVariable)
print("myCoolVariable is: " + str(myCoolVariable))
myCoolVariable = myCoolFunction(myCoolVariable)
print("myCoolVariable is: " + str(myCoolVariable))
myCoolVariable = myCoolFunction(myCoolVariable)
print("myCoolVariable is: " + str(myCoolVariable))

Output

❯ python3 function.py 
myCoolVariable is: 1
myCoolVariable is: 2
myCoolVariable is: 3
myCoolVariable is: 4

Your post is a bit vague, so maybe I'm way off the mark here.

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13963237
Share on other sites

Link to post
Share on other sites

#variables for word lists
wordlist1 = [""]
wordlist2 = [""]
wordlist3 = [""]
wordlist4 = [""]
 

#asks for name
name=input("What is your name?")

#capitalizes first letter of word and decapitalize other letters for name
name = name.capitalize()

#says a greetings
print("Hello", name, "Hope you enjoy!")


#ask what subject they want to do
print("What would u like? Mieliestronk's list of over 58,000 english words [Hard] or ?[option 2]")

#gets input for result
result = input("")

#capitalizes first letter of word and decapitalize other letters for result
result = result.capitalize()


#choices subject:
#To add more results copy one of the options and just change what is in the "" and after the words =

#option 1
if result == "Hard":
    print("You picked: Mieliestronk's list of over 58,000 english words ")
    words = wordlist1
    
    
    
#To add more results copy one of the options and just change what is inside the ""

#option 2    
elif result == "":
    print("")
    words = wordlist2
    
#option 3

elif result == "":
    print("")
    words = wordlist3
    
#option 4

elif result == "":
    print("")
    words = wordlist4

#if all fails terminates program

else:
    print("invalid option")
    print("please start ")
    quit()

 

 

Thats the snippet im having trouble with

 

UwU

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13964567
Share on other sites

Link to post
Share on other sites

22 hours ago, WereCatf said:

.....aaaand you never mention the error you get. Wow, that's very fucking useful.

 

I assume it's that print-clause where I use an f-string. Well, that's easy to fix: just replace the print-clause with something else! The print-clause wasn't the point there, the point was demonstrating how declaring the variable before the loop fixes the issue.

 

Also, quote people if you reply to them. I didn't know you had replied until just now, because you didn't quote and thus I never got a notification about it.

sorry forgot to send that, 

attached is the error

Error.PNG

UwU

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13964568
Share on other sites

Link to post
Share on other sites

42 minutes ago, Spoiled_Kitten said:

sorry forgot to send that, 

attached is the error

The error is rather self-explanatory: fix your indentation.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13964631
Share on other sites

Link to post
Share on other sites

1 minute ago, Spoiled_Kitten said:

i try but it still doesnt work.

You have both spaces and tabs for indentation in your code and you need to use only one or the other -- either replace all the tabs in your indentation with spaces, or all the spaces with tabs.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1239121-python-return-back/#findComment-13973230
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×