unexpected Indent and break out of loop in game code [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i am new to python as you may be able to tell with this question. I am currently building a Rock, Paper, Scissors game to later include into a bigger program i am working on in python 3.4. the problem i am having is in the code listed below.
def computerPlayer(): #randomly selects a rock paper or scissor for computer hand
c = random.randint(0, 2)
if c==0:
y=('rock')
if c==1:
y=('scissors')
if c==2:
y==('paper')
return y
in front of the bottom line return y i am getting a unexpected Indent error, i have tried correcting this over the past day now with no results, if i move it forward i get 'return' outside function, but when i move it back i get the unexpected indent, I am honestly at a complete loss here and im not sure where to go. Any help is great thanks.
the above problem is now fixed, but i know have a break outside of loop error. it is appearing at the end of my code now. any help is great thank you.
again = raw_input('do you wish to try again? (yes\no)\n :') #Ask the user if they want play again
if again == ('yes') or again == ('sure') or again == ('okay'):
print ('')
elif again == ('no') or again == ('nah') or again == ('nope') or again == ('screw you') or again == ('screw it'):
print ('FINE THEN!!! =^( \n (Enter>>>game()<<< if you change your mind)')
#breaks the loop
break
game()

Try this:
def computerPlayer():
'''
Randomly selects a rock paper or scissor for computer hand
'''
c = random.randint(0, 2)
if c == 0:
y = ('rock')
if c == 1:
y = ('scissors')
if c == 2:
y = ('paper')
return y
Indentation is important in python, it shows where your methods and control flows start and end. In your previous code, the if statements were not indented under the method and so python could not tell that it was apart of the computerPlayer() function.
According to PEP8 ( a style guide for python ) proper indentation is 4 spaces. For more information on PEP8 and its view on indentation, check here:
http://legacy.python.org/dev/peps/pep-0008/#indentation

Related

"The cheater's coin" python riddle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I tried for hours to solve this python riddle in my level of knowledge and I don't know what to write past the thinking part "I need to make tails more frequent by using +1 to something" maybe. The riddle goes like that:
import random
def broken_coin():
if random.random() <= 0.9:
return "Heads"
return "Tails"
using no random source part of the function(which you can't edit), and turn the coin into a standard one: if you print the code the chances for heads will be 50% and the chances for tails will be 50%
thanks early to anyone who commented :)
EDIT: added what was my idea
If I read your problem correctly, you need to provide a method to compensate for the broken coin producing a relatively fair amount of results. With that instead of calling the broken_coin() function every time directly, one could call a function that calls the function and every other time returns the reverse result to the calling function.
After reading the comments about what could or could not be used, I've updated my sample code.
import random
def broken_coin():
if random.random() <= 0.9:
return "Heads"
return "Tails"
def fix_coin(j):
if j == 0:
return broken_coin()
coin = broken_coin()
if (coin == "Heads"):
return "Tails"
else:
return "Heads"
for x in range (100):
print(fix_coin(x %2))
See if this more closely fulfils the spirit of the problem.
Regards.

How can I debug an if statement that isn't working? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I do not know what I did wrong.
def main(): # store anything to then replay code.
import time
math = int(input("Hi there, what is 32 + 16? =")) # asking what 32+16 is.
if math == "48":
print("Correct!") #this is the if statement that isn't working, suppose to say correct if the input is 48.
else:
print("Not quite..") # this would come up instead of 'correct' if I would put 48.
time.sleep(2) # a delay
restart=input('Do you wish to start again? ').lower()
if restart == 'yes': # if the player wants to play the game again.
main() # to replay code.
else:
exit() # this wouldn't start the game again.
main() # this just starts main.
Change this
if math == "48":
to this:
if math == 48:
You convert the input to int already.
Wouldn't this be better served in a while loop as opposed to being enumerated at runtime?
To answer your question, you've already declared an integer, the quotes make python interpret it as a string.
You were super close but there are a few things that needed tweaking to get it working.
First, you will need to make sure that your main is defined correctly and remove the "**" from your code before the comments.
Second, the reason your code always fails the if statement check for if they got the correct answer is that you are casting the answer to an integer when you get the input, but then comparing it to a string version of 48, deleting the quotations around that should fix the issue as well.
I have implemented these and had the code working in python3 on my system.
Hope this helps (modified code below)
import time
def main():
math = int(input("Hi there, what is 32 + 16? =")) #
if math == 48:
print("Correct!") #This is where I made the change, removing the
quotes that surrounded the 48.
else:
print("Not quite..")#this would come up instead of 'correct' if I
would put 48.**
time.sleep(2)#a delay**
restart=input('Do you wish to start again? ').lower()
if restart == 'yes':#if the player wants to play the game again.**
main()#to replay code.**
else:
exit()#this wouldn't start the game again.**
main()#this just starts main.**

Python is stopping before running a loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm having trouble with this code. It will run normally until it gets to the loop (def Game) and just stops there. I did have it as an if statement but that still didn't work. Please note that this may be a bit messy.
import random
import time
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
def Game():
GameInProgress = ("Yes")
TutorialDone = ("No")
ReplayGame = ("Yes")
#Test purposes
PlayerName = ("Lewis")
print ("Welcome to 'Guess The Word!")
WordSelected=("No")
LettersGuessed=0
print (TutorialDone)
EnterName = input("Would you like to enter your name?").title()
def Game(): is not a loop, it is a function it does not execute until you call it.
you can call a python function in this way
Game()
if you want to call the same function again and again simply you can call the function inside a for or while loop:
while(condition):
Game()
if your are very beginner follow some tutorials
https://www.tutorialspoint.com/python/python_functions.htm

Syntax error with If statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm just learning Python and right now i'm making a very simple rock, paper, scissors game where the user picks one choice and the computer randomly picks another then the program compares the two and says who won.
My code looks like this:
print ('Rock, Paper, Scissors! The game of random guessing!')
print (input('Please hit enter to begin'))
choice = input('Choose Rock, Paper, or Scissors: ')
print('You decided on: ', choice)
import random
'''random gives this program the ability to randomly choose from a list'''
ComputerChoiceOptions = ['Rock', 'Paper', 'Scissors']
ComputerChoice = random.choice(ComputerChoiceOptions)
print('The computer went with:', ComputerChoice)
if choice = ComputerChoice
Winner = 'Tie'
Print(Winner)
My question is specifically with this bit
if choice = ComputerChoice
My debugger gives me a syntax error with this and I'm not sure why.
If statements (and other control blocks) require a colon and indentation. Also, test for equality by using a double '='.
Example:
if choice == ComputerChoice:
assign = 0
Based on the question I'm assuming you are learning programming for the first time, as your problem is one of the first concepts any programmer must learn.
You need to use a == instead of a = when comparing two things in an if statement. When you use a = it assigns the item.
Also, since you are using Python, you will need to indent the body of the if statement.

Problems about python if condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
question1:The if condition is not working. Every time it quits
question2:(for i in range(1, ret[0]+1):
NameError: name 'ret' is not defined)
Your indentation seems to be way off. It should be the same amount throughout the code. PEP 8 suggests four spaces for indentation.
You are probably receiving an IndentationError.
You seem to mention that "Every time it quits". This is expected. Your code, pp.quit() will quit the program if action does not equal either 'stat', 'list', or 'retr', which is what is happening.
Here is a simplified version of what you have:
action = ""
if action == "stat": # Not true, action == ""
# stuff
elif action == "list": # Not true, action == ""
# stuff
elif action == "retr": # Not true, action == ""
# stuff
else: # Looks like this is where we will end up
exit()
It's no surprise you are quitting every time, since you have hard coded a condition to make it quit every time.
You say if you remove the action = "" you get a NameError saying action is not defined... that is because you never set it to anything... I'm not sure what you expected the if block to do as written. You need something like this:
action = a_function_that_gets_info_from_user_and_returns_a_string()
This will set action to something that might pass your if block.
As a side note, you should't do screenshots for your questions. Instead, copy/paste; it's the polite thing to do. Now I have to hand type your code to illustrate what's wrong instead of copying it myself.

Categories

Resources