Glitch with code? - python

I have this code. If you run it everything works fine if you follow the instructions. However I want to dummyproof it but when you enter too make troops and then you fix your mistake you get an error after fixing the mistake when functions restarts itself.
Please take a look and help me fix it.
import time
warriors = 100
def deploy(): #Calls fighters to front lines
amount = input('How many warriors would you like to send to the front lines? Your limit is %i warriors. Keep in mind that enemy invaders have been spotted inside your base. You must keep 10 warriors in base at all times. ' %(warriors))
try:
amount = int(amount)
except ValueError:
print('Please use numbers.')
time.sleep(1.5)
deploy()
if amount <= warriors:
print (type(amount))
elif amount > warriors:
print("You can't send that many warriors. You only have %i warriors." %(warriors))
time.sleep(1.5)
amount=0
deploy()
else:
print("You did something wrong. Try again.")
time.sleep(1.5)
deploy()
fighters = deploy()
warriors = warriors - fighters

You shouldn't use recursion (e.g. a function calling itself repeatedly) to try and do validation. For some general examples of good patterns for this, the canonical question is a good start. In your case I might refactor slightly.
import time
warriors = 100
def deploy():
while True:
amount = input("...") # your text here
try:
amount = int(amount)
except ValueError:
print("Please use numbers.")
# move the time.sleep to the end
else: # only execute if the try block succeeds
if amount > warriors:
print("You can't send that many warriors. "
"You only have %i warriors." % warriors)
else:
# everything went right!
print(type(amount)) # why are you doing this...?
return amount # did you forget this in your sample code?
# if you get here: something broke
time.sleep(1.5)
That said, this is kind of ugly since it's so deeply nested. Remember the Zen: "Flat is better than nested." Let's refactor a bit to make a new function that does the validation for us.
import time
warriors = 100
def int_less_than(prompt, ceil, type_error_msg=None,
value_error_msg=None, callback=None):
"""Returns a validated integer
input(prompt) must be less than ceil. Print to console a std error msg
if none is specified. If you specify a callback: run the callback if any
errors are detected.
"""
while True:
user_in = input(prompt)
try:
user_in = int(user_in)
except ValueError:
print(type_error_msg or "You must enter a number")
else:
if user_in > ceil:
print(value_error_msg or "You must enter a number "
"less than {}".format(ceil))
else:
return user_in
if callback is not None:
callback() # lets us insert a time.sleep call
def deploy():
amount = int_less_than("How many warriors would you like to...",
warriors,
callback=lambda: time.sleep(1.5))
return amount

Related

Unit Test functions which include user input, while loops and whose input is another functions output - Python

I have made a program for an ATM machine, which checks the pin that a user inputs, allows the user to enter an amount to withdraw if the pin matches and then withdraws the money from the account_balance. However, each function is seemingly dependent: the return of one function is the parameter of another. This is confusing to test each function separately and when I try to do so, the whole program runs and asks for input from each function.
As my get_pin and get_amount function also have while loops with user input, I feel like this has added another complication to the way I need to unit test.
Is there a way to do this?
Or do you think it is best to refactor my code to remove user input and while loops and just have parameters in each individual function?
The test I would like to check would be:
withdraw_cash function - to check if the correct amount is withdrawn
get_pin function - check if the pin matches
get_amount function - check incorrect amount is added bringing up an error message.
My atm program is:
user = {
'pin': 1234
}
def withdraw_cash(amount):
balance_account = 100
if amount > balance_account:
raise ValueError("You don't have sufficient balance to make this withdrawal")
else:
new_balance = balance_account - amount
return new_balance
def get_pin():
count = 0
to_exit = False
while (count < 3) and (not to_exit):
try:
pin = int(input('Please enter your four digit pin: '))
except ValueError:
print("Please enter correct pin")
count += 1
if pin != user['pin']:
print("Pin does not match.. Try Again")
count += 1
else:
return get_amount(pin)
if count == 3:
a = '3 UNSUCCESFUL PIN ATTEMPTS, EXITING \n !!!!!YOUR CARD HAS BEEN LOCKED!!!!!'
return a
def get_amount(pin):
while True:
try:
amount = int(input("Enter the amount of money you want to withdraw: "))
except ValueError as v:
print(f"Enter correct amount: ")
else:
return withdraw_cash(amount)
try:
get_pin()
except ValueError as v:
print(f"ERROR: {v}")

How would I effectively lock a room for a text based game?

I'm using python 2.7 for this game. I have looked at other answers and questions similar to this but since I'm a python beginner with some experience with python I don't really understand some of the answers and some of the answers I saw weren't that clear. Because of the current code I have, it created a problem where when I got the item necessary to unlock the room it says the room has not been found. This is the code:
## checks which connecting rooms player.currentRoom has available and updates currentRoom on player choice.
def navigate(player,enemyToken):
os.system("clear")
# print available rooms to navigate to
i = 0
for room in player.currentRoom.connectingRooms:
print(str(i) + ": " + room.name)
i = i + 1
try:
navigate = input("Choose room to navigate to [type its number]: ")
# change players current room to selected room and contains the locking code after the "and".
if(navigate <= len(player.currentRoom.connectingRooms) and player.items[0].unlocks == room == True):
player.currentRoom = player.currentRoom.connectingRooms[navigate]
else:
print("room not found!")
time.sleep(1)
except NameError:
print("that was not a number!")
time.sleep(1)
except SyntaxError:
print("that was not a number!")
time.sleep(1)
except IndexError:
print("that was not an option!")
time.sleep(2)
how would I make a lock that would succesfully lock the player from accessing that room untill they have grabbed the object that is required to open this room. If you need more code to help me I would provide it.
Based on my comment, this code should word(Untested as I do not know where the locking mechanism should go)
Code:
x = True
while x = True:
if LockRoom != RoomUnlock:
Code here
else:
Code here when you unlock the room
x = False
player.items[0].unlocks == room == True
This part looks weird to me. player.items[0].unlocks == room should be sufficient to check if the two numbers match.

Why does it keep saying "Syntax error" when I use multiple "if" loops?

I tried to program a "make your own adventure" story with python, and I tried using multiple "if" loops. However, the program keeps saying "syntax error." Can someone tell me why this is happening and state a possible solution?
a = eval(input("Enter 0 to go on the ladder, and 1 to try out you new pair of plunger boots: "))
if a == 1:
print("You put on the boots and climb on the side of the ship. You put one foot forward, then the next, then you try to take a step.")
time.sleep(10)
print("However, the boots won't budge.")
time.sleep(2)
print("MISSION FAIL")
time.sleep(1)
else:
print("You climb up the ladder.")
time.sleep(2)
print("You see a guard at the top armed with an automatic rifle.")
time.sleep(4)
print("You can use the grenade in your pocket, or you can take him out with your pack of C4")
time.sleep(5)
b = eval(input("Enter 0 for grenade, 1 for the pack of C4: ")
if b == 0:
print("You throw the grenade up.")
time.sleep(3)
print("You hear a smack, and you see the grenade coming back down at you.")
time.sleep(4)
print("MISSION FAIL")
print("Mental note: These guards are known for their fast reflexes.")
time.sleep(3.5)
It's a missing ")" on your b = eval(...) line.
should be
b = eval(input(...))
and it's
b = eval(input(...)
Thus, syntax error.

Looping functions together for an FSM

As many of you have probably seen and/or coded, I'm trying to make a finite state machine; 5 states and 9 transitions, to determine if someone is laughing. I don't necesarrily want the entire answer, cuz I do want to figure out as much as I can by myself. However, the professors and TA's aren't always very quick or helpful when answering, and this place is way more useful anyway.
I just want to be able to type "ha" or "ho" until a "!" is entered, saying "you laughed." Any other characters will end it. There are 3 functions to make; the get_ch(), the find_state(), and the main(), which drives both of the other ones. Here's what I have:
def get_ch(): # prompt for the input in a loop
ch = input("Enter a character or press the Return key to finish: ")
if len(ch) > 1:
print("Invalid input, please try again.")
else:
return ch
def find_state(state, ch):
if state==1:
if ch=="h" or ch=="H":
state=2
return state
else:
state=5
return state
if state==2:
if ch=="a" or ch=="A" or ch=="o" or ch=="O":
state=3
return state
else:
state=5
return state
if state==3:
if ch=="!":
state=4
return state
if ch=="h" or ch=="H":
state=2
return state
else:
state=5
return state
def main():
print("I can recognize if you are laughing or not.")
print("Please enter one character at a time.")
# initialize the variables, for example:
ch=get_ch()
string=""
state=1
#call the functions in a loop
while get_ch():
if len(ch)<1:
print("\nYou entered", string)
print("You are laughing.")
print("You are not laughing.")
else:
if state<4:
find_state(state,ch)
string+=ch
continue
if state==4:
print("\nYou entered", string)
print("You are laughing.")
else:
print("\nYou entered", string)
print("You are not laughing.")
main()
The issue I'm having is that the functions aren't "talking" to each other. I don't know if this is because they are not properly looped in the main() or if the issue lies within the individual functions themselves (such as calling back on get_ch() while in find_state()).

errors with Elif expected indented block

I'm trying to create a menu for my application, the menu has 4 options and each of these options should return with the correct information when the user has entered the chosen value. i keep getting an error with the Elif statements.
I am a newbie so please understand where am coming from.
much appreciation.
when i indent the while ans: i will receive an error says invalid syntax after indenting the elif ans==2.
elif ans==2 <--- this error keeps saying indention block error or syntex invalid when i indent it.
def print_menu(self,car):
print ("1.Search by platenumber")
print ("2.Search by price ")
print ("3.Delete 3")
print ("4.Exit 4")
loop=True
while loop:
print_menu()
ans==input("Please choose from the list")
if ans==1:
print("These are the cars within this platenumber")
return platenumber_
while ans:
if ans==2:
elif ans==2:
print("These are the prices of the cars")
return price_
elif ans==3:
print("Delete the cars ")
return delete_
elif ans==4:
return Exit_
loop=False
else:
raw_input("please choose a correct option")
You have a while loop without a body. Generally speaking, if there is an indentation error message and the error is not on the line mentioned, it's something closely above it.
loop=True
while loop:
print_menu()
ans = int(input("Please choose from the list"))
if ans==1:
print("These are the cars within this platenumber")
# return some valid information about plate numbers
elif ans==2:
print("These are the prices of the cars")
# return some valid information about pricing
elif ans==3:
print("Delete the cars ")
# Perform car deletion action and return
elif ans==4:
# I am assuming this is the exit option? in which case
# return without doing anything
else:
# In this case they have not chosen a valid option. Send
# A message to the user, and do nothing. The while loop will run again.
print("please choose a correct option")
Also, your code is a bit confusing to me. It looks like you're going to return car_ no matter what, which means your loop will only execute once. Also, = is assignment and == is equality. Be careful.

Categories

Resources