How do I make this so that way whenever the user says no it will stop or terminate the program? Currently, it's not doing that - while-loop

def Main():
global more
InitializeReport()
TeamCode()
more = input("Do you have more to enter?")
while (more != "N" or "No" or "n" or "no"):
ProcPlyr()
more = input("Do you have more to enter?")
CalcAvg()
DisplaySummary()

while (more == "N" or more == "No" or more == "n" or more =="no"):
exit()

Related

Pysimplegui and Pygame merge

hi I am trying to merge a pygame program with pysimplegui.
i'm looking for a way of grabbing the downstroke of a keyboard key. at the moment i can only see an upstroke of the key no matter what im doing.
I have sucessfully created pysimplegui programs but this is the first time i'm trying to merge with pygame.
window is setup by
window = sg.Window('UNICHALL V2.0',
Win_layout,
border_depth =20,
resizable = True,
keep_on_top = False,
finalize=True,
return_keyboard_events=True,
use_default_focus=False)
and in my pygame program i use
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
stop = pygame.time.get_ticks()
delta = stop - start
key = event.key
if key == 27:
sys.exit(1) ...
i can't get these two work in harmony, they either internally loop forever or simply stop the program dead.
Any help appreciated.
If you just need to detect when key pressed and released, try to bind the event '<KeyPress>' and '<KeyRelease>', not set option return_keyboard_events=True of sg.Window.
Example Code
import PySimpleGUI as sg
layout = [
[sg.InputText('', key='-INPUT-')],
[sg.Button('Add Binding'), sg.Button('Remove Binding')],
]
window = sg.Window('Test Program', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event in ('Press', 'Release'):
e = window.user_bind_event
action = 'pressed' if event == 'Press' else 'released'
print(f'char:{repr(e.char)} | keycode:{e.keycode} | keysym:{e.keysym} | keysym_num:{e.keysym_num} {action}')
elif event == 'Add Binding':
window.bind("<KeyPress>", "Press")
window.bind("<KeyRelease>", "Release")
elif event == 'Remove Binding':
window.TKroot.unbind("<KeyPress>")
window.user_bind_dict.pop("<KeyPress>", None)
window.TKroot.unbind("<KeyRelease>")
window.user_bind_dict.pop("<KeyRelease>", None)
window.close()

Pygame not processing input

When Key is pressed on start menu nothing happens
def wait_for_key(self):
wating = True
while wating:
self.clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
wating = False
self.running = False
if event.type == pg.K_SPACE:
wating = False
You are checking for event.type == pg.K_SPACE which is incorrect. What you want to check for is event.type == pg.KEYDOWN. Once you know it's a key down event, then you can check for event.key == pg.K_SPACE.

while loop end everything after it's complete

So I'm trying to make a lottery program where you pick a animal, letter, and number, those are put into a array and compared to another that has the parts chosen by random.
Testing with what I call the animal round.
I have a while loop for invalid entries, that it will not move on till one of the four animals is accepted. But when it does that, the variable invalid goes to false and the coding after it doesn't get used at all. I had this problem last night, and when I finally gave up and went to bed, I decided I'll write it out on flowgorithm (If you haven't heard it makes a flow chart and you can go through programming with it step by step).
I made it, and it worked like expected, I copy and paste it over, and I get the exact same problem as last night.
Here is the code.
#import library
import random
#get variables
game = True
invalid = True
animalarray = [""]
animalarray.append("tiger")
animalarray.append("cow")
animalarray.append("turtle")
animalarray.append("bird")
lotteryarray = [""]
#game loop
#animal round
print("Pick a animal: ")
print("tiger")
print("cow")
print("turtle")
print("bird")
print(" ")
lotteryarray[0] = input()
#while loop for invalid entry
while invalid == True:
if lotteryarray[0] == "tiger" or lotteryarray[0] == "cow" or lotteryarray[0] == "turtle" or lotteryarray[0] == "bird":
invalid == False
else:
print("Invalid entry!")
lotteryarray[0] = input()
print(" ")
print("You chose " + lotteryarray[0])
game == False
And this is all I get in the shell:
Pick a animal:
tiger
cow
turtle
bird
tiger
the tiger there is what I put in, it isn't being printed.
And here is the flowgorithm, like I said, in flowgorithm this works.
flowgorithm of lottery game
I figured it out.
I printed what invalid was after it was meant to change to False and it didn't change, I changed the two equal signs to one and it worked and changed the value.

Variable Being Evaluated as Anything

I am having a problem with the following code. This is supposed to be the beginning of a small text based game. I want to take the users input and then output a message (such as a help menu, or the start of the game).
def start (): # This runs when the program starts
print("THE LAST RANGER")
print ("Type Start to Begin")
print ("Type About for Credits")
print ("Type Help for Instructions")
prompt_start()
def Waking_up(): # This should run when the user enters "Start"
print ("SIR... Sir. We need to get you out of here, come on.")
def About(): #This should run when the user enters "About"
print ("Welcome to THE LAST RANGER created by Michael Frazer.")
#add more to this at some point
def Help(): #This should run when the user enters "Help"
print ("This is the help menu")
def prompt_start(): #This function takes the users input
global input_0
input_0 = input()
start () #This runs the "start" function defined in the first lines
if input_0 == "Start" or "start": #These should evaluate the users input
Waking_up ()
elif input_0 == "About" or "about":
About ()
elif input_0 == "Help" or "help":
Help ()
else:
print ("Enter Valid Command")
My problem is that the input variable is being evaluated as literally anything. For example the line
if input_0 == "Start" or "start": #These should evaluate the users input
Waking_up ()
Runs the "Waking_up" function even when the input is not equal to "Start". If the user enters something like "Help", the program seems to think that "Help" is equal to "Start" and runs the waking up function. I've tried to debug this with 3 friends and we are all out of ideas. Thanks for any help.
"start" is evaluating to true because you don't have the full expression. Try:
if input_0 == "Start" or input_0 == "start": #These should evaluate the users input
Waking_up ()
Just change the following:
if input_0 == "Start" or input_0 == "start":
Waking_up ()
elif input_0 == "About" or input_0 =="about":
About ()
elif input_0 == "Help" or input_0 == "help":
Help ()
else:
print ("Enter Valid Command")
This will work as you want and will only evaluate the corresponding input(s) instead of "Start" all the time.
Depending on what you're doing with the program, consider filtering the user's input as well (such as converting the input string to .lower() and only have your if/elif/else commands accept lowercase input -- may save you some time. Just a suggestion from a fellow python newbie.

How to prevent infinite loop in Boolean while loop?

I have code that goes like:
maybeYes = raw_input("Please enter Yes to continue.")
if maybeYes != "Yes":
print "Try again."
# ask for input again
else:
pass
What do I fill in where I want to make it ask for input again?
you should just do the raw_input directly in a loop.
while True:
result = raw_input("...")
if result != "Yes":
print "Try again."
continue
else:
break
This will loop printing wrong until the user types yes, Yes, YEs, YES, yEs or yeS as the input is converted to all uppercase before checking against YES, then your code can continue on...
while raw_input("Please enter Yes to start: ").upper() != 'YES':
print 'Wrong'
print 'Correct'
#Carry on here
Output:
Please enter Yes to start: nowg
Wrong
Please enter Yes to start: wggwe
Wrong
Please enter Yes to start: Yes
Correct
It looks to me like you WANT an infinite loop here.
maybeYes = raw_input("Please enter Yes to continue.")
while maybeYes != "Yes":
maybeYes = raw_input("Please try again.")
However, you can always add a counter/escape.
maybeYes = raw_input("Please enter Yes to continue.")
attempts = 0
while maybeYes != "Yes" and attempts < 10:
maybeYes = raw_input("Please try again.")
attempts += 1