I would like to achieve the following.
I have a proof of concept I am working.
I have Individual "Named RFID"Cards, then I have "Action RFID Cards".
So I might have cards like this:
Names
John - 12345
Mary - 12346
Actions
Start Work - 111
Finish Work - 222
Lunch - 333
So John Swipes his own card, then swipes an action card, which logs his action.
-Start Script
-Wait for User Card Input
-Once Input Received and Validated
- Wait for Action Card Input
- Start Timer
- Wait until Action Card Input matches a valid Action
- If a match, exit back to the main loop
- If no match, wait for one minute, then exit
-Continue Main Loop
I am reusing code from :
How would I stop a while loop after n amount of time?
import time
timeout = time.time() + 60*5 # 5 minutes from now
while True:
test = 0
if test == 5 or time.time() > timeout:
break
test = test - 1
and a Python Game example which waits and loops forever playing the game
https://dbader.org/blog/python-intro-reacting-to-user-input
My code for testing is as follows (I am not doing a card or action lookup at this point, expecting the user to be 12345 and card to be 54321: (the requirement for four spaces for indent has possibly broken Python Indent)
#
# Guess My Number
#
import random
import time
# Set our game ending flag to False
game_running = True
while game_running:
# Greet the user to our game
print()
print("I'm thinking of a number between 1 and 10, can you guess it?")
# Have the program pick a random number between 1 and 10
#secret_number = random.randint(0, 10)
secret_number = 12345
card_number_list = 54321
# Set the player's guess number to something outside the range
guess_number = -1
# Loop until the player guesses our number
while guess_number != secret_number:
# Get the player's guess from the player
print()
guess = input("Please enter a number: ")
# Does the user want to quit playing?
if guess == "quit":
game_running = False
break
# Otherwise, nope, the player wants to keep going
else:
# Convert the players guess from a string to an integer
guess_number = int(guess)
# Did the player guess the program's number?
if guess_number == secret_number:
print()
print("Hi you have logged on, please swipe Card- if you don't Swipe - will timeout in 1 minute!")
timeout = time.time() + 60*1 # 1 minutes from now
while True:
test = 0
if test == 1 or time.time() > timeout:
card = input("Please enter your card number")
card_number = int(card)
if card_number == card_number_list:
print("Thanks for your card number")
test = 1
break
test = test - 1
# Otherwise, whoops, nope, go around again
else:
print()
print("You need to use your wrist band first...")
# Say goodbye to the player
print()
print("Thanks for playing!")
But instead of exiting, the script waits...
Any feedback appreciated - I have basic python skills and am trying to reuse existing code where possible (with thanks to the creators!).
The python input() function will always wait for response from the keyboard before returning. Take a look at this answer for a technique to accomplish what you want.
Related
I'm currently making a scoring system for an event and I'm having trouble using while loops as way to validate user input. For example I want to force the user enter their name so it can be added to a spot for a sport like tennis which I used a dictionary for. Everytime I am done asking them for their user input the while loop keeps apearing leading to a never ending while loop. Here's what I've done.
```solo_player = {
"Contestant 1":{"Player 1":[],"Score":[],"Event":[]},
"Contestant 2":{"Player 2 ":[],"Score":[],"Event":[]},
"Contestant 3":{"Player 3":[],"Score":[],"Event":[]},
"Contestant 4":{"Player 4":[],"Score":[],"Event":[]}}
def individual():
solo_name = False
while not solo_name:
solo_name = input("What is your name:")
print(""" \nIndividual Menu and Available Spots\n
1) Player 1
2) Player 2
3) Player 3
\n4) Go back to Main Menu\n""")
solo_menu = ["1","2","3","4"] #valid choices for this menu
solo_menu = False
while not solo_menu:
solo_menu = input("Please enter your choice here:")
if solo_menu == "1":
solo_player["Contestant 1"]["Player 1"].append(solo_name)
print(solo_player["Contestant 1"])
print("Thank you for taking the spot of Player 1")
solo_menu = True
elif solo_menu == "2":
solo_player["Contestant 2"]["Player 2"].append(solo_name)
print(solo_player["Contestant 2"])
print("Thank you for taking the spot of Player 2")
solo_menu = True
else:
print("Enter a value between 1-4)
solo_menu = False```
My ideal output would be that the while loop will stop after I picked one spot from the available spots. Sorry for the long if/else statements I'm quite new to it so I don't know how to make it shorter
EDIT: I fixed the issue. It was caused by calling the individual func outside my menu def.
Here's what I believe you are after.
A couple of changes:
Your while loops can contain the validation that you are wanting to perform as their condition. If your validation becomes more complex then you can consider moving back to a boolean to break the while loop, but it feels overly complex for your requirements.
Removal of the if statements. The whole idea of a dictionary is that you can specific a key and get a value, so having an elif for every potential key is overly cumbersome. Just concatenate the validated user input to derive the key.
solo_player = {
"Contestant 1":{"Player 1":[],"Score":[],"Event":[]},
"Contestant 2":{"Player 2":[],"Score":[],"Event":[]},
"Contestant 3":{"Player 3":[],"Score":[],"Event":[]},
"Contestant 4":{"Player 4":[],"Score":[],"Event":[]},
"Contestant 5":{"Player 5":[],"Score":[],"Event":[]},
"Contestant 6":{"Player 6":[],"Score":[],"Event":[]},
"Contestant 7":{"Player 7":[],"Score":[],"Event":[]}}
solo_name=""
while solo_name == "":
solo_name = input("What is your name:")
print(""" \nIndividual Menu and Available Spots\n
1) Player 1
2) Player 2
3) Player 3
4) Player 4
5) Player 5
6) Player 6
7) Player 7
\8) Go back to Main Menu\n""")
#prompt for player slot
solo_menu_options = ["1","2","3","4","5","6","7","8"] #valid choices for this menu
solo_menu=""
while solo_menu not in solo_menu_options:
solo_menu = input("Please enter your choice here:")
solo_player["Contestant "+solo_menu]["Player "+solo_menu].append(solo_name)
print(solo_player["Contestant "+solo_menu])
print("Thank you for taking the spot of Player "+solo_menu)
i would like to have a countdown for my rocket to life off but i just cant make it decrease the number it only stays the same can someone help me?
print("program is writen by someone")
name = input("program is runned by")
print(name,",if you are a pilot of the project please type pilot for the next question. If you are other please type your reason/role for u being here")
x = input("what is your reason for u being here")
if x == 'pilot':
print (name,"ok wlecome to the crew")
h = input ("is the hatch closed? please answer closed or no")
if h == 'closed':
import time as t
second = int(input ("very good please enter the needed for the countdown"))
for i in range(second):
print (str(second - 1)+ "second remaining \n")
t.sleep(1)
print ("time is up")
else:
print ("please check if the hatch is close")
else:
y = input("are you 600meters away from the spacecraft?")
if y == 'yes':
print ("have a nice time watching the show")
else:
print("please stand back untill u are 600 meter away from the aircraft")
You're not using the loop variable and printing the same second-1 value every time. You can use a decreasing range, e.g.:
for i in range(second, 0, -1):
print(f'{i} seconds remaining')
time.sleep(1)
or simply a while-loop:
while second:
print(f'{second} seconds remaining')
second -= 1
time.sleep(1)
(Notice that time.sleep() is called inside the loop body)
I am writing a rock, paper, scissors game in Python but my code doesn't work as it should. I'm new to Python so please let me know if my code isn't formatted corectly. The game runs fine, assuming you enter one of the already existing answers. However, if you enter one that is different, the code seems to loop randomly after the 'end()' function is executed.
Here is my code:
# imports needed files
from random import randint
import time
# creates a function that ends the game
def end(cpuScore,playerScore):
time.sleep(1)
cont = input("Would you like to play again? (y or n)\n")
if cont=="y":
time.sleep(1)
start()
else:
print("Well... That's a bit rude.")
# creates a function to play the game
def rps(cpuScore,playerScore,num):
# loops code 3 times (unless 'num' is different)
for x in range(num):
num-=1
# creates options
options = ["rock","paper","scissors"]
# picks a random choice for cpu
cpu = options[randint(0,2)]
# asks the player to choose
player = input("rock, paper or scissors?\n")
# why not gun?
if player=="gun":
result = "w"
elif player==cpu:
result = "d"
elif player=="rock":
if cpu=="paper":
result = "l"
if cpu=="scissors":
result = "w"
elif player=="paper":
if cpu=="scissors":
result = "l"
if cpu=="rock":
result = "w"
elif player=="scissors":
if cpu=="rock":
result = "l"
if cpu=="paper":
result = "w"
# if they choose something other than rock, paper, scissors or gun
else:
print("That's an invalid input!")
# adds one to num so that this round is not counted as one of the 3
num+=1
# plays the game again with the amount of rounds remaining
rps(cpuScore,playerScore,num)
# tells the player how they did
if result=="w":
playerScore+=1
time.sleep(1)
print("Fine! You win! Your silly " + player + " beat my " + cpu + "!!!")
if result=="l":
cpuScore+=1
time.sleep(1)
print("Ha! Sucker!! My epic " + cpu + " smashed your measly " + player + "!!!")
if result=="d":
time.sleep(1)
print("Ah! We drew by both choosing %s! Like they say, great minds think alike!" % cpu)
# announces the scores
print("You are on %s and the computer is on %s!" % (playerScore,cpuScore))
# ends the game after 3 rounds
end(cpuScore,playerScore)
# creates the funtion that sets the variables and starts the game
def start():
result=""
cont=""
cpuScore=0
playerScore=0
rps(cpuScore,playerScore,3)
# begins the game
start()
Thanks
Basically your rps function loops num times, with num = 3 initially. If the user enters an incorrect input, you call back the function, which starts the whole process again, in a new context, for num+1 times.
Thus if you answer wrong the first time you have at least six games to play: four new added and the two initial ones you didn't try to play.
My advice try first to do a program that do one and only one iteration of the rock-paper-scissor game. Adding more iteration is a simple fact of adding a global loop.
I'm trying to make a little text based game, but I am having trouble with a while loop. I have experimented for many hours now! I would much appreciate it if you could kindly help me out. Thank-you for reading :D
I basically want it so that the user has to press a button before the timer runs out, and if he doesn't do it in time then the bear eats him. :')
Here is my code:
import time
cash = 0
def dead():
print("You are dead!")
main()
points = 0
def adventure_1(inventory, cash):
points = 0
time1 = 2
if time1 < 3:
time.sleep(1)
time1 -= 1
bear = input("A bear is near... Hide Quickly! Enter: (C) to CLIMB a Tree")
#Ran out of time death
if time1 == 0:
dead()
#Climb away from bear
elif bear == 'c' or 'C':
print("Your safe from the bear")
points += 1
print("You recieved +2 points")#Recieve points
print("You now have : ",points,"points")
adventure_2()#continue to adventure 2
#Invalid input death
elif bear != 's' or 'S':
dead()
def adventure_2(inventory, cash):
points = 2
time = 5
In python the input statement makes it so the programs flow waits until the player has entered a value.
if time1 < 3:
time.sleep(1)
time1 -= 1
#line below causes the error
bear = input("A bear is near... Hide Quickly! Enter: (C) to CLIMB a Tree")
To over come this you can use something similar to the code below which is a working example. We can over come the break in the program flow by using a Timer to see if the player has inputted anything, if he has not we catch the exception and continue with the programs flow.
from threading import Timer
def input_with_timeout(x):
t = Timer(x,time_up) # x is amount of time in seconds
t.start()
try:
answer = input("enter answer : ")
except Exception:
print 'pass\n'
answer = None
if answer != True:
t.cancel()
def time_up():
print 'time up...'
input_with_timeout(5)
So as you can see we can over come the issue of waiting for our player to input a value by using a timer to count how long the player is taking, then proceeding to catch the exception from no input being sent, and finally, continuing with our program.
t_0 = time.time()
bear = input("A bear is near... Hide Quickly! Enter: (C) to CLIMB a Tree")
if abs(t_0 - time.time()) > time_threshold:
#player has died
So, I'm just fooling around in python, and I have a little error. The script is supposed to ask for either a 1,2 or 3. My issue is that when the user puts in something other than 1,2 or 3, I get a crash. Like, if the user puts in 4, or ROTFLOLMFAO, it crashes.
EDIT: okay, switched it to int(input()). Still having issues
Here is the code
#IMPORTS
import time
#VARIABLES
current = 1
running = True
string = ""
next = 0
#FUNCTIONS
#MAIN GAME
print("THIS IS A GAME BY LIAM WALTERS. THE NAME OF THIS GAME IS BROTHER")
#while running == True:
if current == 1:
next = 0
time.sleep(0.5)
print("You wake up.")
time.sleep(0.5)
print("")
print("1) Go back to sleep")
print("2) Get out of bed")
print("3) Smash alarm clock")
while next == 0:
next = int(input())
if next == 1:
current = 2
elif next == 2:
current = 3
elif next == 3:
current = 4
else:
print("invalid input")
next = 0
Use raw_input() not input() the latter eval's the input as code.
Also maybe just build a ask function
def ask(question, choices):
print(question)
for k, v in choices.items():
print(str(k)+') '+str(v))
a = None
while a not in choices:
a = raw_input("Choose: ")
return a
untested though
since the input() gives you string value and next is an integer it may be the case that crash happened for you because of that conflict. Try next=int(input()) , i hope it will work for you :)