The major problem I'm having is what occurs when I choose stay on hand_one, and then hit on hand_two.
Instead of asking me to hit or stay on hand_two again, it brings me back to hit or stay on hand_one, when I already chose stay on hand_one, so hand_one should have no more options. This causes issues with multiple print statements occurring and incorrect game play.
What is wrong with my code that it is like causing it to loop back to hand_one.
The full code is here: http://labs.codecademy.com/Bjaf/2#:workspace
Here is the part likely causing the issue.
def hit_or_stay(person):
hit_or_stay = raw_input("Do you want to hit or stay? You can type h or s.")
if hit_or_stay == 'h' or hit_or_stay == 'hit':
deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()
elif hit_or_stay == 's'or hit_or_stay == 'stay':
print "You stayed"
return
else:
hit_or_stay(person)
def number_value_of_hand():
if number_of_hands > 0:
value_of_hand_one = value_of_current_cards(hand_one)
if value_of_hand_one < 18:
print "\n" "You have %i" % (value_of_hand_one)
hit_or_stay(hand_one)
elif value_of_hand_one > 18:
print "You Lose"
return
elif value_of_hand_one == 18:
print "You hit HOT 18!!!"
return
if number_of_hands > 1:
value_of_hand_two = value_of_current_cards(hand_two)
if value_of_hand_two < 18:
print "\n" "Your second hand has %i" % (value_of_hand_two)
hit_or_stay(hand_two)
elif value_of_hand_two > 18:
print "You Lose"
return
elif value_of_hand_two == 18:
print "You hit HOT 18!!!"
return
number_value_of_hand()
Can anyone see why it loops back to give hand_one another option? And possibly how I can fix it? Thanks so much!
Your problem occurs on this step:
hit_or_stay(hand_two)
When you hit on hand_two, your code does this:
deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()
The problem is right there, because number_value_of_hand() brings you back to the beginning of that function, and goes through the hand_one options again.
You will probably have to rewrite your number_value_of_hand() function to include an argument that tells it where to begin (hand_one, hand_two, etc.)
I would probably make a list of hands, and iterate through the list. Then, you could call number_of_hands(hands[i]) to being at the ith hand.
Related
while guess_q != guess_result:
guess_q = input("Guess away: ")
i -= 1
print("You have " + str(i) + " tries remaining")
if i == 0:
print("You lost")
break
print("Congrats!!")
I just started learning Python and I manage to make a guessing game, but the problem is everytime I input the correct answer the tracker that's counting how many tries left is also printed. Is there any way to only print the tracker if the answer is still wrong and not print it when the input is correct?
As mentioned in the comment, your while loop already test if you are giving the right answer, and breaks if you don't have any chances left, so you can move the print after the break. However, if you lose, you will still print "Congrats" with your code right now.
Since you have 2 possible conditions for stoping your loop, and they should give different printed output, you could do a "while true" loop with the to breaking conditions inside:
i = 5
while True:
guess_q = input("Guess away: ")
i -= 1
if guess_q == guess_result:
print("Congrats!!")
break. # If you win, breaks the loop
if i == 0:
print("You lost")
break # If you run out if trial, break the loop
else: # If you still have trials, print the message and keep going
print("You have " + str(i) + " tries remaining")
I'm a fairly new programmer and I'm trying to do a type of BlackJack were when you get 21 or over 21 the code "break". Or the code do not keep on running.
I have tried exit() and sys.exit() but at the end this keeps popping up "An exception has occurred, use %tb to see the full traceback.
SystemExit" and I wonder if there is another type of command to break the code from running but without an error message.
Player = input()
if Player == "Hit me":
print("You have",x+y,)
if x+y == 21:
print("You win")
sys.exit()
elif x+y > 21:
print("You loose")
sys.exit()
this is a part of it or more so the part that I want to change with the "sys.exit()".
BTW the x and y you see is variables that are assigned random numbers in the beginning.
You shouldn't rely on a forced exit from the program - instead, build-in the exit case to your code. Without seeing your code it's difficult to be specific, but an example structure would be:
score = 0
while score < 21:
score = black_jack(score)
print("You scored {}!".format(score))
Where the black_jack() function contains your code to simulate a round of the card game.
I am a Python newb.
I keep getting indent errors around the if statements of my code.
The first chunk of if/else statements are read fine.
The second chunk results in indent errors.
When I remove it for debugging.
The third chunk (at the end) also returns indent errors.
...but I'm not sure where to find them?
Any suggestions on what's going wrong?
# Begin Fight/Conditions for Ending
while Player.hp > 0 and Marco.hp > 0:
while playerchoice not in [1,2,3,4]:
playerchoice = input("\n Which move would you like to use: ")
marcochoice = random.choice([1,2,3,4])
# Making the Moves - Player Always Goes First (For now!)
if playerchoice == 1:
Player.jabs(Marco)
#print("\n Player - jabs - Debug")
elif playerchoice == 2:
...
else:
#print("Player - Non-Choice - Debug")
# Marco's Turn!
if Marco.hp > 0:
if marcochoice == 1:
Marco.jabs(Player)
#print("Marco - Jabs - Debug")
...
else:
#print("Marco - Non-Choice - Debug")
else:
pass
# Ending Conditional
if Marco.hp <= 0 and Player.hp <= 0:
print("It's a draw!")
...
else:
print("Something has gone horribly wrong!")
It's not the quotation marks. I made that error when transferring the code to stackoverflow (modified the code for readability/compactness).
The error is with the comments by some of the else statements.
Python never reads/executes anything, so it just assumes whatever follows is supposed to be indented.
Once I put pass underneath the else statements, everything cleared up.
Assuming you try to run this exact code you posted, the problem is the comment after you first 'else:' statement. The same happens here:
for i in range(10):
if i in [0,1,2,3,4,6,7,8,9]:
print("found")
else:
#print("test")
print("that could be it")
To run without problems just uncomment the second print statement.
Hope that helps.
It looks like your forgot the double quote
playerchoice = input("\n Which move would you like to use: ")
The color of the text could have helped you ;)
Where can I add a while loop in this code to ask for another move. I really need help I can't figure it out. I want it to re-prompt the move so when the move is invalid it will ask for another move again. And if the move is valid it will ask for the NEXT move?
def valid1(pile1):
move = False
pile1 = 3
if pile1 == 0:
print 'please choose from pile 2 and 3:'
elif pile1 == 3:
move = int(input('please choose the amount you would like:'))
if move > 0 and move <= 2:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
elif pile1 == 2:
move = int(input('please choose the amount you would like:'))
if move > 0 and move <= 2:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
elif pile1 == 1:
move = int(input('please choose the amount'))
if move > 0 and move <= 1:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
else:
print pile2
print valid1(3)
I'm finding it a bit hard to understand your code precisely, so I'm not sure which bit to loop around, but I think if you use the following general principle you'll fix this:
looping = True
while looping = True :
move = int(input('please choose the amount you would like:'))
if move > 0 and move <= 2:
pile1 = pile1 - move
else:
print 'you have entered a invalid move'
return pile1
In the code above, you'll keep looping and so the input prompt will keep being asked after you've entered a move. If you want to break out of the loop when a certain condition is met, then after you've coded the condition put looping = False. In python, the first letter of Boolean values must be capitalized. There are other ways to do this, such as with break statements etc, but this is the method I prefer to use. As I said, I wasn't sure exactly what you wanted looping around in the code, but if you enclose it all in a 'while' statement like above, it should be fixed. Hope this helps!
This is the code i have been using but it is not working as it says that the elif statement is invalid syntax. Can anyone help me because i am a beginner in python.
age = int(input('How old are you?: '))
if age <= 16:
print ('You are too young to enter this section of the program!')
else:
age >= 16
print ('Welcome to the advanced section of the program!')
elif: password = int(input('please enter the password to enter the elite members section: ')):
if password == ('muffin'):
print ('Well done for unlocking the secret part of the program')
else:
print ('You shall not pass!')
elif needs a condition (ie elif 7 > 5: ....) also an elif cannot follow an else and must follow an if or elif condition
in your case your elif has no condition ,so python does not know what to do
your elif also follows your else Im not sure what you actually expect it to do ...
Your elif has no conditional. Else if...what?
else:
age >= 16
This looks like an attempt at an elif. Should this really be
elif age >= 16:
That'd make sense based on your if.
Additionally, elif goes before else. If you are using elif, it needs to occur after the if but before the else:
if ...
elif ...
else ...
As stated above, be sure to have a condition for your elif statement as well as place it between your if and else. You may also run into an issue with your statements when a user's age is 16 as both your if and else technically qualify at this point.