Script stops printing output when input changes - python

I'm making a coin flip game that asks the user "Heads or Tails", and after the user chooses one of the two, it will respond back saying either "correct" or "wrong". When I run my code, it works perfectly fine if I keep repeating either heads or tails. But if I switch it up like something like heads, heads, tails . It won't ask me the question "Heads or Tails" anymore. And what's weird is sometimes after switching it up it will ask but then eventually it stops. This is my code:
import random
coin=["CORRECT!", "WRONG!"]
hot=raw_input("Heads or Tails? \n")
while hot == "heads":
if hot=="heads":
for i in range(1):
print random.choice(coin)
hot=raw_input("Heads or Tails? \n")
while hot =="tails":
if hot=="tails":
for i in range(1):
print random.choice(coin)
hot=raw_input("Heads or Tails? \n")
And then this right here is the problem I'm talking about when I run it
Heads or Tails?
heads
CORRECT!
Heads or Tails?
tails
CORRECT!
Heads or Tails?
tails
WRONG!
Heads or Tails?
heads
After that last heads I put it doesn't say correct or wrong, and sometimes it doesn't show it even earlier when I run it.

You can try implementing the same using:
hot = raw_input('Heads or Tails?\n')
while hot != 'quit':
if hot == random.choice(['heads', 'tails']):
print 'Correct!'
hot = raw_input('Heads or Tails?\n')
else:
print 'Wrong!'
hot = raw_input('Heads or Tails?\n')
This allows the user to continue flipping the coin until the user enters 'quit'.

It isn't strange that your program stops after you stop responding "tails". This is because of basic control flow. You've used five types of control flow in your program.
First, unless told otherwise, control will flow from one statement to the next. So import random happens first, then the assignment of coin, and so on.
Second, you've used function calls such as raw_input() and random.choice(). These run other subprograms, which we don't need to consider very deeply here since all of them return to your main program.
Third came two while loop constructs. These contain some section of code they repeat as long as their condition is satisfied; crucially, they therefore end when it is not. So the first loop actually ends when you respond something other than "heads".
Fourth, you have an if statement within each loop. This runs its contents if a condition is satisfied, just like while, but doesn't repeat. Since the condition you used is exactly the same as the surrounding while, these checks are redundant; we wouldn't be executing this part of the program if the condition wasn't met.
Fifth you used a for statement; these make loops, like while, but process every entry of some iterable. In this case, that served no purpose because there is only one entry in range(1) and you never actually used it. Like the if, it is redundant.
Having deciphered these, we see the control flow moves from "heads" loop to "tails" loop then simply ends, having run out of program. To keep the program running we must have a loop that does not end, for instance:
while True:
if hot=="heads":
for i in range(1):
print random.choice(coin)
hot=raw_input("Heads or Tails? \n")
if hot=="tails":
for i in range(1):
print random.choice(coin)
hot=raw_input("Heads or Tails? \n")
This loop exhibits another issue, namely that nothing checks that the answer entered was either heads or tails. It will therefore get stuck running nothing if we give another answer. You can solve this by ensuring we ask for a guess in every iteration, and for some extra flair, even respond to invalid guesses using else:
while True:
hot=raw_input("Heads or Tails? \n")
if hot=="heads":
for i in range(1):
print random.choice(coin)
elif hot=="tails":
for i in range(1):
print random.choice(coin)
else:
print "I don't know what side of a coin that is."
Note that I moved the raw_input call outside of the conditional sections. Since it was equal for all branches, it doesn't need duplication. I also used elif and else to tie together the conditional sections, ensuring only one of them will run for any iteration (each time through the loop). And about duplication, your two conditionals perform the exact same operation! You've taken a shortcut and not actually calculated which side the coin landed on - only used its odds of success. Let's fix that too.
sides = ("heads", "tails")
while True:
hot = raw_input("Heads or Tails? \n")
if hot in sides:
toss = random.choice(sides)
if hot == toss:
print "Correct"
else:
print "Incorrect"
else:
print "I don't know what side of a coin that is."
Note that there are two if statements on different levels this time, each with their own else. These are independent, aside from the fact that the inner one only runs if the outer condition was true. All sorts of control flow statements (ending in :) can be nested this way.

Related

Why does using loop-only functions in a def section not work, even when in a loop?

I am making a small text-based game in Python. It involves many inputs and so to avoid bugs, there are a few things I have to check every time an input exists. Naturally, to speed up the process I wanted to put the code into a def in order to simplify the writing process. When I put the code in the def, it red underlines the continue and break commands (meaning they are incorrect), and if you run the code using the def name, a Traceback occurs. I have tried putting the def section at the beginning of the program, after the while True: (The program is supposed to run infinitely until a certain action is taken that breaks the loop) I have also made sure to try putting it under any variables referenced and in the loop so that no part of it is not defined and so that everything would work if I were to just put the code in there.
Here is the code I am trying to put into a def.
def input_bugs():
if letter_one.lower() == "done" and total_count == 0:
print("You have to play at least one game!")
continue
elif letter_one.lower() == "done":
break
elif len(letter_one) > 1:
print("Sorry, you gotta pick a single letter, no more. Otherwise, type 'done' to end the game and see your stats.")
continue
Here is the Traceback I get every time I try to run it.
line 20
continue
^^^^^^^^
SyntaxError: 'continue' not properly in loop
At this point, I don't even care if I have to write it out every time, I can just copy and paste. I am simply curious as to why it doesn't work so that I know in the future. In case you could not tell, I am pretty new to programming so I want to learn from any mistake I make. Also sorry if I referred to some things in the wrong way. Hopefully, you understood what I meant.

How to make a Dungeons and Dragons-esque skill check in Python?

I am trying to make a small text-based adventure game in Python IDLE, but I am running into some issues with having the game give a different response if the player rolls above or below the DC of a DND style preception check. I am very new to this so it's probably an easy fix. Here's the code.
I have no idea how to input the code into the question correctly so here is a picture.
BTW I did import random at the beginning of the code its just too far back to include in the screencap.
Your problem is you are not specifying what numbers the random module to choose from, in the line if random.randint > 10:. To fix this, put the numbers you want it to choose between. For example, if you want it to choose a random number between 1 and 20, your line would become if random.randint(1,20) > 10:.
You will also want to do this for the other line, which reads if random.randint < 10:.
welcome to SO,
Please read the minimal reproducible example guide posted in the comments by D.L. First thing I would do is to post the actual code, because if the link expires, then others viewing this post with a similar issue cannot figure out what was the given example, and how it was solved.
With logistics out of the way, to fix the error you are specifically receiving is to put what is rolled in a variable.
Here are a few things I would change to make your code clear
# Some string that receives yes or no
room_2_input = ("...")
# if condition is yes
if room_2_input == 'yes':
# Put it in a variable
roll = random.randint(1,20)
# You do not have to format a string this way, but I think it makes it easier
print('You rolled {}'.format(roll)
# Put this condition within the first if, because you don't
# need to execute it if they do not choose to roll
# Neither of your original condition has inclusivity, so if 10 is rolled,
# your program will do nothing, because neither condition would be met
if roll >= 10:
'''do stuff'''
# I would only include elif < 10 if you are specifically looking for > 2
# outcomes, but your outcome appears to be binary, either above or below 10
else:
'''do stuff'''
The reason you would not do a random.randint(1,20) > 10: check in your second if statement is because you would be executing a different roll than your first one.

Capitals game with dictionary python

I am trying to a make a simple game. I will supply of a dictionary of states and capitals and a list of states. Using loops and conditionals, I will ask the user if they want to learn some capitals and supply the list of states. The state gets removed from the list and the user should be prompted again if they want to play, repeatedly until the list is empty. With my code right now the loop piece works, if keeps asking if they want to play and as long as the user keeps saying yes my code works running till the list is empty and the loop. But when I try to add a layer for if the player says no and break the loop its not doing anything. Thanks in advance for help!!
states3 = ["NH", "MA", "MS", "SC", "HI"]
print("Let's test my geography skills!")
def state3(states3):
state_caps = {"NH": "Concord", "HI": "Honolulu", "SC":"Columbia", "MS": "Jackson", "MA":"Boston"}
play = input("Would you like to learn some capitals:")
while play == "Yes" or "yes":
if play == "Yes" or "yes":
print ("The states I know the captials of are:", states3)
yourstate = input("What state do you want to know the capital of: ")
print("The capital of", yourstate, "is", state_caps.get(yourstate, "That is not a vaild choice"), "!")
states3.remove(yourstate)
play = input("Would you like to learn some capitals:")
if len(states3) == 0:
print ("That's it! That's the end of geography skills")
break
state3(states3)
while play == "Yes" or play == "yes":
if play == "Yes" or play == "yes":
print ("The states I know the captials of..")
.....
....
elif play == "no" or "No":
break
Checking for yes/no is problematic. For example, "YES" is not covered in the above code, yet seems a reasonable response. To distinguish between the two choices, do we need to look at the entire word or only the first letter? (string "slicing")
Such reduces the set of applicable responses to the set: y, Y, n, N. Now, if we apply the string.lower() or string.upper() method*, we are down to two choices.
However, consider this even more closely. Is there really only one user-response that interests us? That the game should continue. Thus, any response other than 'yes' could be considered to mean stop looping!
Another question: once the while-condition has been satisfied and the loop starts to run, is it possible for the first if-condition to be anything other than True? Could the latter be removed then?
Instead of merely adding a "layer", let's review the entire game. How's this for a spec[ification]: repeatedly invite the user to choose a state, and then present its capital.
How do we define "repeatedly", or more precisely, how do we end the looping? There are two answers to that question: either all the states have been covered, and/or the user loses interest (the new "layer"). What happens if we use (both of) these to control the while-loop? (NB reversing the logical-statement) Loop if there are states to review and the user consents. Thus, can the last if-condition move 'up' into the while-condition...
Because you are evidently (enjoying) teaching yourself Python, I have left the writing of actual code to your learning experience, but herewith a few items which may increase your satisfaction:-
in the same way that the 'heading print()' is outside the loop, consider where the farewell should be located...
A list with contents is considered True, whereas an empty/emptied list is considered False. Very handy! Thus, you can ask if states3 (ie without the len()).
also consider the upper()/lower() 'trick' when accepting the state's abbreviation-input.

Exiting a python application for 'GAME OVER'

I would like to know why this code does not work; it should exit at the "GAME OVER" point, but it continues to my next defined function.
I have tried other variations on exit() such as: sys.exit(), quit() and SystemExit.
run_attack = input("What do you do: Run/Attack\n")
run = ['run', 'Run', 'RUN']
attack = ['attack', 'Attack', 'ATTACK']
run_attack = 1
while run_attack < 10:
if run_attack == ("run") or ("Run") or ("RUN"):
print ("You turn to run from the wolf but he quickly pounces
you...")
time.sleep(2)
print("You are quickly ripped apart and just about get to see
yourself be eaten.")
print("GAME OVER")
break
exit() #This is where the game should exit, yet after input it
continues to the next function
elif run_attack == ("attack") or ("Attack") or ("ATTACK"):
print("You brace yourself for a bite and have no time to reach"
"for any kind of weapon form your backpack.")
time.sleep("2")
input("You clock the dog hard, twice on the muzzle.")
print("The dog recoils in pain and retreats back to the woods.")
print("You quickly start running as you assume there will be a den in the woods.")
break
else:
input("Type Run or Attack...")
You have several problems in your code; why did you write this much without testing it?
First, you read the user's input, immediately replace is with 1, and then try to test it (incorrectly) as if it were still a string. Your posted code has several syntax errors, so I have some trouble reproducing the problem. However, the immediately obvious problem is here:
break
exit() # This is where ...
You can't get to the exit statement, as you break from the loop just before you can get there.
I strongly recommend that you back up to a few lines and use incremental programming: write a few lines at a time, debug those, and don't continue until they do what you want.
Also look up how to test a variable against various values. Your if statement is incorrect. Instead, try the list inclusion you're trying to set up:
if run_attack in run:
...
elif run_attack in attack:
...
I took the liberty of rewriting your whole program to show you a few things wrong with it and a few tricks. I've done it without the loop, since you never use it anyway... you can add the while loop later once you've mastered it, but you should really go back to basics on some things here:
run_attack = input("What do you do: Run/Attack\n")
if run_attack.lower() == "run":
print("""some
stuff
with
multiple
lines and GAME OVER""")
exit()
elif run_attack in ("attack", "Attack", "ATTACK"):
print("""some
stuff
with
multiple
lines""")
else:
input("Type Run or Attack...")
Some notes:
Using """ for strings enables you to write multiple lines without multiple print statements
Using str.lower() on strings makes everything easy to compare because you only have to compare it to the lowercase version of each string. However for attack you can notice I used a different inclusion test, without multiple conditions. Either way works here.
Like the other answer here (and many comments), you should use only exit() to leave the program entirely, or only break to exit the loop and continue to other code that's beneath the entire loop.
When you rewrite your loop, with a condition like while number_of_turns < 10 don't forget to add 1 to the number of turns on each loop, otherwise that condition is always True and you'll have an infinite loop...
I'm actually quite surprised this code had any resemblance to the behavior you expected from it, my suggestion is to go back over to the basics of python, learn loops, string methods, basic commands. The rest is already said in the other answer here (which is better than mine, frankly) just wanted to add some ideas.

Python: Break statements for while blocks

import random
def get_num ():
return random.randrange (999,9999)
print ("{}".format (get_num ()))
def get_user_input():
while True:
user_input = input
print("Please enter a four digit number")
return user_input
if False:
print ("Length of string:" , len (str))
Here in this piece of coding I am trying to make a random 4 digit number which will tell user whether or not s/he has guessed the right number (essentially),
specifically though: It will tell the user (at the end of the game) if s/he has guessed certain digits correctly but not which position.
I want 'break' statement to be fitted into this which will separate the while block from the if False. How do I do this correctly? I have tried maany times but I have 4 problems:
1- I don't know where to insert the break
2- When I run the program it doesn't print the second print function.
3- When I run the program it doesn't tell me the length of the string so I don't know if the user is even enterring the correct number of digits.
4- How do I set a limit on python (i.e. how many goes a player can have before the game ends?
I guess you are new to programming and this may be one of your very first codes. It would be great if you start by learning syntax of programming language which you have decided to use as well as working of loops, return statements, etc. I personally preferred reading any basic programming language book. For your case, it would be any book of python which is for beginners. For the sake of completeness, i have added the below code which is probably not exactly what you asked for:
import random
def get_num():
return random.randrange (999,9999)
def get_user_input():
user_input = int(input())
return user_input
while True:
comp_num = get_num()
print("The computer gave: {}".format(comp_num))
print("Your turn:")
user_num = get_user_input()
if user_num == comp_num:
print("Done it!")
break
else:
print("No, it's different. Try again!")
print()
In the above code, there are two functions and a while loop. One of the functions takes input from the user while the other generates a random number. The while loop is set to run for infinite iterations in case the user doesn't give the same input as the computer. As soon as the user gives the same input as the computer (which is displayed on the screen before he is asked to give input), the if condition evaluates to true, some things are printed and the break statement breaks the loop. And since, there is no further code, the program terminates

Categories

Resources