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
Related
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 1 year ago.
Improve this question
def main_menu():
print("WELCOME TO LIBRARY")
print("What would you like to do?")
option=print(input("1. New user \n 2. Existing user ")
if option == 1:
new_user()
elif option == 2:
old_user()
else:
print("please choose a correct value")
main_menu()
def new_user():
print("new user")
def old_user():
print("Old user")
main_menu()
Here is a picture of the error, it says syntax error missing ")" for function call and missing "else" for the first one at "if":
the line
option=print(input("1. New user \n 2. Existing user ")
is missing a closing )
it should be
option=print(input("1. New user \n 2. Existing user "))
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 1 year ago.
Improve this question
I have a simple chatbot with the following code
import random
human_input = input('Talk to me: ')
continue_dialogue = True
greeting_inputs = ("hey", "good morning", "good evening", "morning", "evening", "hi", "whatsupp")
greeting_responses = ["hey", "hey hows you?", "*nods*", "hello, how you doing", "hello", "Welcome, I am good and you"]
def generate_greeting_response(input):
for token in input.split():
if token.lower() in greeting_inputs:
return random.choice(greeting_responses)
while continue_dialogue:
for token in human_input.split():
if generate_greeting_response(human_input) is not None:
print("Chatterbot: " + generate_greeting_response(human_input))
input('Talk to me again: ')
else:
print("Chatterbot: Bye")
continue_dialogue = False
What I wanted was that if human_input is recognized inside greeting_inputs for the conversation to keep going and if it isn't, then the conversation would stop. But the code above never stops the conversation even if I input nonsense. Why is my else statement never activating?
Update input('Talk to me again: ') to
human_input=input('Talk to me again: ')
Since next time it asks, you haven't given it a variable to assign the input, it keeps repeating
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 4 years ago.
Improve this question
while True:
inp = raw_input()
if inp == "":pres= input("What would you like to know about me? AGE, JOKE, FACT")
if pres in {'AGE'}:
print("I was birthed from my mother 87 years ago.")
if pres in {'JOKE'}:
print("Where do polar bears keep their money?")
import time
time.sleep(2)
print("In a snow bank!")
if pres in {"FACT"}:
print("Hippopotamus's have pink spit!")
I am a student and for my school project have to do a code and for some reason I can't figure out how to get the chatbot to answer the question when someone puts in AGE, JOKE or FACT. Insteas, it just repeats the question when I press enter.
You are asking for input in your while True loop. It will never exit, because True is... always true. You need to put the if statements inside the loop for them to execute as well.
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 6 years ago.
Improve this question
Here is my code
import time
print("------------------------------------------")
print(" Welcome to Senpai Quest")
print('------------------------------------------')
time.sleep(3)
print("")
print('You have English next, there is a test. If you hit yes .you go to
class and you get +1 charisma and Knowledge.')
print("")
print('If you hit no, you will skip class and get +1 Courage and +1 risk.')
ans1=str(input('Do you Take the english test? [Y/N]'))
if ans1== ['y']:
print("It works")
else:
print("Woo Hoo!")
When it asks the question and for the 'y' it just goes straight through to "woo hoo!". What i would like it to do is to print "It works" but if you type n it just goes to woo hoo. Please help
This should work:
ans1 = input('Do you Take the english test? [Y/N]').strip()
if ans1.lower() == 'y':
print("It works")
else:
print("Woo Hoo!")
I suggest using the distutil's strtobool in order to cover all cases
from distutils.util import strtobool
ans1=input('Do you Take the english test? [Y/N] ').strip()
if strtobool(ans1):
print("It works")
else:
print("Woo Hoo!")
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