I have this nested loop in my project (it's a lot more complicated of course I just simplify it so you can see what I mean). I know there is no label and goto in python, I just want to show what I want to do.
From line #goto third I want go back to place where you can see #label third.
I tried different setups of my loops but they never do what I want
import time
onoff = "on"
t=0
while onoff == "on":
#label first
for x in range (5):
print("first loop")
time.sleep(1)
for y in range (5):
print("second loop")
time.sleep(1)
p = 0 #for testing
t=0 #for testing
if p != 5:
if t == 0:
print("third loop")
time.sleep(1)
p2 = 5 #for testing
t=0
if p2 != 5: #label third
if t == 0:
print("go back to first loop")
time.sleep(1)
#goto first
else:
print("lock")
#lock.acquire()
else:
if t == 0:
print("go back to third loop")
p2 = 3
time.sleep(1)
#goto third
else:
print("lock")
#lock.acquire()
else:
print("lock")
#lock.acquire()
Every path in this nested loops seems to work fine but I want my loop to go back to #label third from #goto third and it goes back to #label first. How can I change my loops to make it possible?
Actions like goto first which break 'for' loops are evil in many ways. While loops are more elegant, but maybe a 'state machine' like solution is better for you. Something like:
state = 0
while is_on:
if state == 0: # do outer loop things
<do things>
state = 1 # to do inner loop things
elif state == 1:
n = 0
# do inner loop things
n += 1
if n == 5:
state = 0
elif state == 2: # do even more nested things
p = 0
if <some condition>:
state = 0
p += 1
if p == 5:
state = <whatever>
A state machine permits much more flexibility. Also, it won't cause as much indentation as nested loop. If the complexity gets larger, there are some libraries which can help you. Interesting links on Finite State Machines (FSM):
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/StateMachine.html
https://www.python-course.eu/finite_state_machine.php
Related
I just made a little program to make some plots with tkinter in python.
And I'm trying to make the app to create png file not overlapping the previous one.
So like, if it exists, rename it as ~ (1).png kinda thing.
But when I run os.path.isfile or os.path.exists, the app just freezes. Like, endless hourglasses turning and saying not responding, you know.
So, this is the function I binded with the button.
def get_information_S():
global nation_list, checked_list, output, year_menu, auto_flag, df
year = year_menu.get()
checked_list.sort()
nations = []
if auto_flag == 0:
for n in range(len(nation_list)):
nations.append(nation_list[n][0].get())
else:
pass
if len(year) == 0:
output_log("연도를 선택해주세요.\n")
elif len(checked_list) == 0:
output_log("hs코드를 선택해주세요.\n")
else:
output_log("\n\n분석 연도 : "+str(year)+"\n분석 품목 : "+str(checked_list)+"\n분석 국가 : "+str(nations)+"\n분석을 실시했습니다.\n")
for x in range(len(checked_list)):
os.makedirs("result", exist_ok=True)
os.makedirs("result\\single", exist_ok=True)
os.makedirs("result\\single\\hs_"+str(checked_list[x]),exist_ok = True)
df_test = df[df.hs6==int(checked_list[x])]
df_test = df_test[df_test.year==int(year)]
file_loc = "result\\single\\hs_"+str(checked_list[x])+"\\hs_"+str(checked_list[x])+"_"+str(year)+'.png'
f = -1
while True:
f+= 1
if os.path.isfile(file_loc) == True:
if f == 0:
file_loc = file_loc.replace('_.png','_('+str(f)+').png')
else:
file_loc = file_loc.replace('('+str(f-1)+')','('+str(f)+')')
continue
else:
break
plain_graph(df_test, file_loc, checked_list[x], nations)
Well, it's my first time dealing with GUI.
Is there a way I can use os.path.isfile or os.path.exists? Or any walkaround?
Thank you!
The initial value of file_loc is something like "result\\single\\hs_xxxx_2021.png" (assume year is 2021), then in the first iteration of the while loop, if f == 0 will be evaluated as True and so
file_loc = file_loc.replace('_.png','_('+str(f)+').png')
will be executed, but nothing will be replaced as _.png is not found in file_loc and so file_loc is kept as initial value and continue line will proceed to next iteration. As the file_loc does not change and f is increased, the else part
file_loc = file_loc.replace('('+str(f-1)+')','('+str(f)+')')
will be executed but again nothing is replaced as the search pattern is not found in file_loc.
Then same result happens for every iteration followed and make the while loop an infinite loop.
So the mentioned line should be changed to:
file_loc = file_loc.replace('.png','('+str(f)+').png')
Whenever I run this is get:
Traceback (most recent call last):
File "main.py", line 130, in <module>
while is_playing_cg:
NameError: name 'is_playing_cg' is not defined
I want the user to be able to press 1 or 2 to select which game mode to use and then once pressed it starts. I don't know why it's doing this. Whenever it's fixed it should run through just fine.
New edit
Now it just loops and says 1 or 2 over and over again.
import random
is_playing_cg = False
is_playing_hg = False
def game_select_screen():
#Game Select Screen
print("""
._________________________________.
| |
| ~ Welcome To Guess-A-Number ~ |
| ~~~ |
| ~ Press 1 OR Press 2 ~ |
| You ^ Guess | PC ^ Guess |
|_________________________________|""")
selecting = True
while selecting:
print()
game_mode = input("1 OR 2: ")
try:
int(game_mode)
except ValueError:
print("This is not a Number.")
else:
game_mode = int(game_mode)
if game_mode == 1:
is_playing_hg = True
elif game_mode == 2:
is_playing_cg = True
#Defining Random Number for human guess
def play_human_guess():
num = random.randint (1,10)
print()
print("Im Thinking of a Number 1 Through 10.")
print("You Have 3 Chances.")
chances = 3
game_fisnished = False
#Game is playing (Human Guesses)
while not game_fisnished:
guess = input("> Take A Guess: ")
#Accept only numbers
try:
int(guess)
except ValueError:
print("This is not a Number.")
else:
guess = int(guess)
if guess < num:
chances -=1
if chances == 0:
print()
print("Sorry You Guessed Too Many Times.")
game_fisnished = True
elif chances !=0:
print()
print("You Guessed Too Low. ("+str(chances)+") Chance(s) Remaining.")
elif guess > num:
chances -=1
if chances == 0:
print()
print("Sorry You Guessed Too Many Times.")
game_fisnished = True
elif chances !=0:
print()
print("You Guessed Too High. ("+str(chances)+") Chance(s) Remaining.")
else:
print()
print("Congradulations, You Won!")
game_fisnished = True
#Game Ended
def end():
print()
print("Thanks For Playing!")
#Setting up for computer guess
def play_computer_guess():
print()
print("Pick a Number 1 Through 10")
print("I Have 3 Chances to Guess Your Number.")
chances = 3
game_fisnished = False
#Game is playing (Computer Guess)
while not game_fisnished:
guess1 = input("Is your number 5?")
#Show Game Select Screen
game_select_screen()
while is_playing_cg:
#Start Game
selecting = False
play_computer_guess()
answer = input("""
Do You Want to Play Again? (y/n) : """)
if answer == "n":
is_playing_cg = False
while is_playing_hg:
#Start Game
selecting = False
play_human_guess()
answer = input("""
Do You Want to Play Again? (y/n) : """)
if answer == "n":
is_playing_hg = False
end()
The variable is_playing_cg is only available in the "block" that creates it.
Block is function / loop / if statement / etc.
In your program you need to initialize the variable globally so you can call them in multiple functions.
Good luck!
You are defining is_playing_cg inside of a conditional statement at the top of your code. So if that option is not selected, then when you get to the latter conditional statement, it has never heard of that variable.... and it is not defined in the namespace. So you could either define it at the top and give it a default (False) or more better, because you only have 2 options, just use one variable to control the computer / human.
Here is a toy example:
selection = int(input('enter 1 for human, 2 for computer: '))
if selection == 1:
human_play = True
elif selection == 2:
human_play = False
else:
# make some loop that asks for input again or such...
pass
# later on...
if human_play:
# ask human for input...
else:
# have AI make turn
#if needed, you can also handle special cases like this:
if not human_play:
# do something unique to computer turn ...
Additional info...
So you got bit by the scope of the variables in your update. You are defining these variables inside of a function and when you put the defaults outside of the function, they are not in the same scope, so whatever you do inside the function is lost. So, you need to change your function into something that returns the mode you want, and catch that in the function call like such:
def ... :
# input ...
if game_mode == 1:
human_playing = True
selecting = False
elif game_mode == 2:
human_playing = False
selecting = False
return human_playing # this will return T/F back to the function call
And then later:
#Show Game Select Screen
human_playing = game_select_screen()
while not human_playing:
#Start Game
selecting = False
play_computer_guess()
answer = input("""
Do You Want to Play Again? (y/n) : """)
if answer == "n":
is_playing_cg = False
while human_playing:
#Start Game
This (above) works for me, but there are still other logic errors popping up! :) Have fun
This particular error is probably there because you have not defined is_playing_cg as global before using it in your function game_select_screen. Simply put global is_playing_cg at the start of your game_select_screen function to tell python you want to use the global variable instead of creating a scoped variable.
So I'm new to python and I tried making a random number game generator as an exercise. You need to guess the random number in a limited number of tries. Here's my code.
import random
rand_num = random.randint(1,6)
print("\nNUMBER GUESSING GAME")
lives = 5
def guess():
guess = int(input("Input guess: "))
return guess
print(rand_num) #to see the correct answer
x = guess()
if x == rand_num:
print("Good job! YOU WIN.\n")
else:
while x != rand_num:
if x == rand_num:
print("Good job! YOU WIN.\n")
break
lives -= 1
print("Incorrect. Please try again")
guess()
if lives == 0:
print("YOU LOSE.\n")
break
So if your first guess is wrong, and you found the right answer in the subsequent tries, the game won't recognize it.
Thanks in advance.
You were on the right track. In the future, walk yourself through the program thinking what each line is doing and whether the sequence makes logical sense.
There was only one actual error in your code: you didn't collect a new x value in your code when calling guess in the while loop. The other elements were correct, just out of order. Here is a more logical order, with the collecting issue fixed,
import random
rand_num = random.randint(1,6)
print("\nNUMBER GUESSING GAME")
lives = 5
def guess():
guess = int(input("Input guess: "))
return guess
print(rand_num) #to see the correct answer
x = guess()
if x == rand_num:
print("Good job! YOU WIN.\n")
else:
while x != rand_num:
# 1) Notify the user
print("Incorrect. Please try again")
# 2) Decrement remaining guesses
lives -= 1
# 3) Check if maximum number of guesses
# reached - end if yes
if lives == 0:
print("YOU LOSE.\n")
break
# 4) Get a new guess
x = guess()
# 5) Compare
if x == rand_num:
print("Good job! YOU WIN.\n")
You may want to notify your player what are the ranges of numbers to guess. You can also extend this exercise, and code several difficulty levels, each with a different range (e.g. simple level 1-6, medium 1-20, etc.), to be chosen by the player.
As #Stef mentioned in the comment, it is technically cleaner and more common to include the guess number (lives) condition as part of the while loop condition,
import random
rand_num = random.randint(1,6)
print("\nNUMBER GUESSING GAME")
lives = 5
def guess():
guess = int(input("Input guess: "))
return guess
print(rand_num) #to see the correct answer
x = guess()
if x == rand_num:
print("Good job! YOU WIN.\n")
else:
while (x != rand_num) and (lives > 0):
# 1) Notify the user
print("Incorrect. Please try again")
# 2) Decrement remaining guesses
lives -= 1
# 3) Get a new guess
x = guess()
# 4) Compare, enf if correct
if x == rand_num:
print("Good job! YOU WIN.\n")
# If ending because no more guesses left
if lives == 0:
print("YOU LOSE.\n")
I use brackets for each condition, it is not necessary here and a matter of style. I like to separate conditions this way.
Also, the "YOU LOSE.\n" part is now printed at the end, if the all the guesses were exhausted.
One note: with your code as it is now, your player actually gets to guess 6 times, once in the beginning and 5 more times in the while loop.
I found this code, and I'm having trouble understanding the lines 'user_hand = rps[int(user_input) - 1]' , 'com_hand = choice(rps)' , ' if rps_dict[user_hand]['strong'] == com_hand:' , 'print_result(**game_result)' , '
Why should I subtract 1 from user_input? Why put rps inside brackets next to choice? Why do you win 'if rps_dict[user_hand]['strong'] == com_hand ? Why if user_hand equal to com_hand? It doesnt make sense to me.. I know '**game_result' means it's a kwark, but I dont know why I need to declare it as a kwarg. I'm sorry if these questions seem stupid, but I'm frustrated.
from random import choice
rps = ('rock', 'paper', 'scissors')
rps_dict = {
'rock': {'strong': 'scissors', 'weak': 'paper'},
'paper': {'strong': 'rock', 'weak': 'scissors'},
'scissors': {'strong': 'paper', 'weak': 'rock'}
}
def print_result(user_score, com_score, win, lose, tie):
print('Result:', end=' ')
if user_score > com_score:
print('You win!')
elif user_score < com_score:
print('You lose...')
else:
print('Draw.')
print('--------------------')
# Print scores
print(f'Your score: {user_score}')
print(f'Computer score: {com_score}')
print('--------------------')
# Print statistics
print(f'Win: {win}')
print(f'Lose: {lose}')
print(f'Tie: {tie}')
def play(rounds):
game_result = {
'user_score': 0,
'com_score': 0,
'win': 0,
'lose': 0,
'tie': 0
}
while rounds > 0:
user_input = input(
'Enter your choice (1: Rock, 2: Paper, 3: Scissors, 0: Quit): ')
# Check whether the input is in the options
if user_input in ('1', '2', '3'):
rounds -= 1
user_hand = rps[int(user_input) - 1]
com_hand = choice(rps)
# user_hand is strong against com_hand
if rps_dict[user_hand]['strong'] == com_hand:
game_result['user_score'] += 1
game_result['win'] += 1
result = 'You win!'
# user_hand is weak against com_hand
elif rps_dict[user_hand]['weak'] == com_hand:
game_result['com_score'] += 1
game_result['lose'] += 1
result = 'You lose...'
# Tie
else:
game_result['tie'] += 1
result = 'Tie.'
print(
f'You -> {user_hand}. Computer -> {com_hand}. {result}')
elif user_input == '0':
break
else:
print('Invalid input!')
print()
print_result(**game_result)
if __name__ == "__main__":
print('Welcome to Rock-Paper-Scissors Game!')
try:
rounds = int(input('How many rounds you want to play? '))
play(rounds)
except ValueError:
print('Please input a valid number!')
This is an interesting question, and I'll do my best to discuss the lines that you said you have trouble with:
user_hand = rps[int(user_input) - 1]
The user_hand variable is used to store the choice the user inputs. user_input is the text the user directley entered. This will be saved as a string, so the code stransforms it into an integer with the int class. Next, it subtracts one from the user input (computers count from zero rather than 1). Then, it grabs the element from the list that has that index. For example, if I entered one, it would grab the list item at index 0 ("rock").
com_hand = choice(rps)
This line here is used to gather the computer's choice. As you can see from the first line, the choice function from the random module is imported directly. This allows you to use the function without specifying the module it came from. The choice function selects a random item from the specified list, which is the same rps list as before.
if rps_dict[user_hand]['strong'] == com_hand:
The if statement here gathers data from the rps_dict and compares it to the computer's hand. The rps_dict is a dictionary that contains data on which hand beats or loses to another. To translate the if statement into simpler english, it means if the hand the user's hand would beat (which can be found with rps_dict[user_hand]["strong"]), is the computer's hand, the user will win. In addition, to avoid confusion, the == operator check's for equality, and doesn't assign the variable to com_hand.
print_result(**game_result)
Here you said that you don't understand why the parameters are passed in this way. You don't really need to do it in this format, but the person who created the script decided to (possibly as it is simpler to read).
Thank you, and if you have any other questions, please comment and I'll do my best to answer them!
I'm pretty new to python and I have been trying to make this simulator that creates a simulator for "games" of craps. My professor wanted it to be made in the most basic form possible which is the reason for having a function for rolling one dice then another. For some reason I can't get my simOneGame function to cooperate and I can't even get it to print a value when I assign a variable to it although it should print a 1 or 0. Here is my code:
def main():
x = eval(input("how many games will be played?: "))
y = simNGames(x)
print(y)
def simOneGame():
win = 0
lose = 0
x = rollDice()
if x == 2 or x == 3 or x == 12:
lose += 1
elif x == 7 or x == 11:
win += 1
y = rollDice()
while y != 7 or y != x :
y = rollDice()
if y == x:
win += 1
elif y == 7 :
lose += 1
if win > lose:
return(1)
else:
return(0)
def simNGames(n):
wins = 0
loses = 0
x = simOneGame()
for i in range(n):
if x > 0:
wins += 1
else:
pass
frac = wins / n
return float(frac)
def rollDice():
return rollDie() + rollDie()
def rollDie():
return randrange(1,7)
I don't get an answer unless input a number greater than 100 and it's always a 1.0
There are several issues I see with your code.
First off, your simNGames only actually simulates one game, but then it adds up that game's results n times. You need to move your x = simOneGame() line inside the loop so that it gets called repeatedly.
There's a separate issue with your simOneGame function's logic. A single craps game is either won or lost, there's no scoring. That means you don't need to be adding up win and loss values. You should change all the places where you do win += 1 to do return 1 instead, and all the places that do lose += 1 should be return 0 (this means you can get rid of the rest of the code dealing with the win and lose variables too).
You also might consider returning True and False rather than 1 and 0, but this might have been decided by your teacher, and it's more a design style point than an error.
Finally, I think the while loop in simOneGame needs an and rather than an or to make the logic work correctly.