Python Tutorial - Looping your code back (to the beginning or middle) using a procedure
ฝัง
- เผยแพร่เมื่อ 2 ต.ค. 2024
- A quick tutorial on how to loop your code back to the middle or beginning using a procedure. This is useful if you want to reload / re-run the script, module or code block.
You're literally a god my dude, you saved all my noobish code!. I wrote a giant if elif else code with a lot of shit, and I didn't know how to properly loop till I saw this video. Not even hours and hours of research did what your 6 min video did. Thanks... I REALLY apreciate it.
Good luck for your work ^^
Thank you for a clear and useful tutorial with no superficial talk only relevant stuff
Bro you deserve more subs than you have you saved my life
Thank you, this really helped me out!
thanks for this helpful information
thanks so much bro, saved me so much time
thank u bro, u helped me a lot
Nice man, I got a task and thanks to you I did it!
TL;DW
-Define (make a function) the program that you want to repeat or not repeat (depending on user input)
-Make a variable input at the end of the function you just made, preferably an integer input for simplicity
-Make either, 1 if statement and an elif/else, or make 2 if statements deciding whether the repeat variable was yes or no
-If the repeat variable was yes then put the function name under the corresponding if statement, and if the repeat variable was no then put an "exit()" under the statement
-Lastly, not under any function, call the function
Useful video! Thanks
earned urself a sub
thank you so much
you're a lifesaver
thank you
thank you
thank youuuuuuuuuuuuuuu
Thanks you, very helpful script to know
thank you!
This will crash if user repeats too many times because it is recursive.
yeah!
Thanks so much!
Simple and Great... (Y)
Thank you...Gooooodd!!!!
thank sir
sheesh moment
While this does work, doesn't it expose your program to Python's recursion limits?
Originally I was scripting in CMD and made heavy use of 'goto' commands, then switched to Python, did the exact same thing as in this video, but realized that Python does remember which functions were called, and if you continue to call, without ever using "return" or the like, you will hit recursion limit.
crystal clear and helpful no 15minutes of messing with my head, straight to the point, thank you so much
Thank you very helpful appreciate it 💖 🙏🏾👊🏾
This would only works if you were using only one def function. Otherwise it will loop like forever 😢
Doesn’t work 😐
it does. watch out for indent. its important
def main():
print("type one of the following : goat, wolf , cabadge").lower()
operator = input("What did the man chose?")
if operator == "goat":
print("excelent!")
elif operator == "wolf":
print("worng!")
elif operator == "cabadge":
print("worng!")
else:
print("WRONG!")
repeat = input("Would you like to try again?").lower()
if repeat == "yes":
main()
else:
print("bye")
exit()
main()
where am i wrong? is not coming back to start?
Repeat = input("Would you like to retry?").lower()
if Repeat == 'yes':
main()
else:
print('Thanks for playing')
exit()
main()
You need to make the text indent and define "main" at the start of where you want it to restart from.
#!/bin/python3
from random import randint
import time
import functools
import operator
import random
import math
print('Welcome to multiplication questions.')
ans=input('Are you ready (yes/no) : ')
if ans.lower() == 'yes':
start_time = time.time()
def main():
score = 0
# Game Logic
while True:
difficulty_setting = 2
difficulty_progression = math.floor(score/10)
overall_difficulty = difficulty_setting + difficulty_progression
number_list = []
for x in range(overall_difficulty):
value = random.randint(1,9)
number_list.append(value)
answer = functools.reduce(operator.mul, number_list, 1)
print('Multiply these numbers', number_list)
guess = int(input())
if guess == answer:
score = score + (1* overall_difficulty)
continue
else:
print('Game Over! The answer was', answer)
elapsed_time = time.time() - start_time
print('Your score was', score, 'in only', elapsed_time)
break
Repeat = input("Would you like to retry?").lower()
if Repeat == 'yes':
main()
else:
print('Thanks for playing')
exit()
main()
That is an example of a code I used.
@@aspire_minister5426 man it still doesn't works :(
here is my code : ( it take an input from the user then he write a letter then the program tells the number ) :
############################
def main():
yeslist=["yes","y","YES","y","yeah","ok"]
name=input("enter your name:")
letter=input("enter a letter:")
if letter in name:
print(name.index(letter))
else:
print("something went wrong :(")
repeat=input("do u want to restart ? :").lower()
if repeat==yeslist:
main()
else :
print("ok, bye :(")
exit()
main()
Hello ye I tried this and when i run the program nothing gets printed out? please help I have no clue whats wrong
now i have a looping calculator
Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print("Thank you so much!")
Thanks a lot for the help 👍🏾😊
I HAVE BEEN LOOKIN FOR THIS FOR HOURS THANK YOU!!
and i can apply this anywhere?
Thank you very much bro❤❤❤
Thank You very much!!
Thanks a lot bro!!
THANK YOU SO MUCH BRO
Thank you very much for this tutorial!
Thank you for a clear and useful tutorial with no superficial talk only relevant stuff
I keep getting an error saying "yes" is not defined when I enter yes for restart
you have to write
if restart=="yes"
main()
u cannot code noob
just like your dp u dumbfuck
put "yes" in a variable
Unleash your Tech ????
This is nice
thanks! If it weren't for this i'd still be stuck getting errors trying to loop with while and for. deserves a like and sub fam.
Thank you for this video! I'm trying to get my script to repeat 10 times. How can I set a number for the number of times it repeats?
Copy Paste your main code 10 times
SUPER SIMPLE
Justo lo que necesitaba gracias!! // exactly what I needed thanks!!
You just made this assignment less awful.
thanks, nice IDE bro
Thank you this is exactly what I needed! Subbed
Very helpful had to make some changes to match my needs but was still very helpful
thanks very helpfull appreciate it ( share done)
Thank you you’re a legend good sir
Thanks man, Appreciated
you are a god!!
VERY Good explanation and helped me a lot!
i absolutely love you
Thank u sir
helped me alot! thank you!
Thanks mate it worked!
What if i want multiple "yes's" (e.g. yes,ye,yep) do i have to use elif??
use the or state mant
Yes
very clear, very cool
Bro If you want to get more subscriber then just hide the subscriber, people don't follow channel which have low subscribers
WOW... THANKS DAMN YOU GOOD. I HAVE BEEN STRUGGLING FOR DAYS AND YOU JUST MADE MY LOFE SIMPLE AS HELL.