Weird problem with calling functions in Python - python-3.x

I'm just trying to write a code for myself and I have problem with calling a specific function in my code and it is weird because I already have 2 more functions just like this one and they do their job correctly check it out
import random
name = ("aghayan","jafari","panahi","kashkool")
word = random.choice(names)
dash_guess = "-" * len(word)
guesses_left = 5
class hangman():
def Entrance():
print(" one of your python classmates was an undercover cop and set a ")
print(" trap for our Boss Mohammad Esmaili!'THE CARTEL KING' so they arrest him .")
print(" we just need that snitch name and your the only person of that")
print(" class that we have access to , so your going to tell us the snitch")
print(" name or i will hang you my self and you got only 5 chances to ")
print(" tell me his or hers name or you'll die")
print()
def repeat():
your_choice =input(" so will you help me or you want to die ? 'yes' or 'no' : ")
if your_choice == "yes":
print("Good it seems you have someone waiting for you and you want to ")
print("see him/her again , you better be telling the truth or i,ll go ")
print("and pay a visit to your love")
core_game(guess)
elif your_choice == "no":
print("ok good choice , it will be my pleasure to kill you ")
print("________ ")
print("| | ")
print("| 0 ")
print("| /|\ ")
print("| / \ ")
print("| ")
print("Adios my friend , i hope you rest in peace in HELL")
exit()
else :
print(" it seems the noose tightens around your neck and its getting")
print(" hard to talk but i just need 'yes' or 'no' for answer")
repeat()
repeat()
Entrance()
def core_game(guess):
while guesses_left > 0 and not dash_guess == word:
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
core_game(guess)
game = hangman()
It's not complete but the question is when I enter 'yes' it should take the program to def core_game() but it give me error that " core_game is not defined ".

This section is your problem:
def core_game(guess):
while guesses_left > 0 and not dash_guess == word:
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
core_game(guess)
The lack of indent on the last line drops you out of the class definition. In other words, you're calling core_game from the global scope (where it's not defined) rather than from within the class (where it is defined).
Python is picky with indenting and formatting; I'd advise you to take some time to learn how to correctly format your code for Python, which will not only help you reduce errors but will also make your code significantly easier for you and anyone else to read.
Your solution is to remove the core_game(guess) call entirely. You don't need it, because you're already calling Entrance(), and that calls core_game at the correct points for you.
You've also got another issue - your core_game method has a guess parameter, but it's not necessary and it's making it hard for you to call it correctly:
def core_game(guess):
while guesses_left > 0 and not dash_guess == word:
# On this line, you're overwriting the value of the guess parameter
# before you've actually read it. Hence, you don't actually
# need that parameter at all.
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
And, where you call it:
if your_choice == "yes":
print("Good it seems you have someone waiting for you and you want to ")
print("see him/her again , you better be telling the truth or i,ll go ")
print("and pay a visit to your love")
# At this point, guess is not defined, so you're not passing anything
# to the core_game function.
core_game(guess)
Given that (a) you're not passing anything, and (b) you never actually use the parameter, you can just remove it.
After all the suggestions above, here's how your code looks:
import random
name = ("aghayan", "jafari", "panahi", "kashkool")
word = random.choice(names)
dash_guess = "-" * len(word)
guesses_left = 5
class Hangman():
def entrance(self):
print(" one of your python classmates was an undercover cop and set a ")
print(" trap for our Boss Mohammad Esmaili!'THE CARTEL KING' so they arrest him .")
print(" we just need that snitch name and your the only person of that")
print(" class that we have access to , so your going to tell us the snitch")
print(" name or i will hang you my self and you got only 5 chances to ")
print(" tell me his or hers name or you'll die")
print()
def repeat():
your_choice =input(" so will you help me or you want to die ? 'yes' or 'no' : ")
if your_choice == "yes":
print("Good it seems you have someone waiting for you and you want to ")
print("see him/her again , you better be telling the truth or i,ll go ")
print("and pay a visit to your love")
core_game(guess)
elif your_choice == "no":
print("ok good choice , it will be my pleasure to kill you ")
print("________ ")
print("| | ")
print("| 0 ")
print("| /|\ ")
print("| / \ ")
print("| ")
print("Adios my friend , i hope you rest in peace in HELL")
exit()
else:
print(" it seems the noose tightens around your neck and its getting")
print(" hard to talk but i just need 'yes' or 'no' for answer")
repeat()
repeat()
def core_game(self):
while guesses_left > 0 and not dash_guess == word:
guess = input("so tell me that snitch name letter by letter : ")
if guess != 1:
print("NOPE , i need you to spell the name of that rat")
game = Hangman()
game.entrance()
I've also applied some stylistic corrections and corrected the indentation here. You've got a logic bug left, as well, but I'll leave that as an exercise for you to figure out.

Related

trying to get the "print("Which will you choose?")" to go to a new set of choices which will lead to other choices

The assignment is to create a text based game. The goal of this game is to open the locked door inner to access the rest of the house. When the door has been unlocked the player can return to the entrance room and open the door to the rest of the house.
in each room (the pantry and the kitchen) there are a set of locks of which you must turn to the corect postions to unlock the central door).
Now i think im on the right track however, as the question states, im stuck on trying to get teh final part of the ##if option == "1":## to get to a set of choices.
Here is what i got..im not looking for answers just help to be lead in the right direction.
do i define a new set of functions like #def choices():#, then list the available options?
import random
import time
def displayIntro():
print("Welcome to the")
time.sleep (1)
print("Dungeon of Doom")
print()
time.sleep (1.5)
displayIntro()
#need to defin the path choices and what they lead to# also need to make sure it validates the inputs so no errors is user inputs a false input#
def choosePaths():
path = ""
while path != "1" and path != "2" and path != "3":
path = input("Which path will you choose? (1,2 or 3): ")
return path
def checkPath():
print("You head down the hallway into the pantry.")
time.sleep(2)
print("")
time.sleep(2)
print("")
print()
time.sleep(2)
def menu():
print('Choose your option')
time.sleep (1)
print("[1] Enter The Dungeon")
print("[2] Run away.")
menu()
option = ""
while option != "1" and option != "2":
option = input("Enter The temple or run away? (1 or 2): ")
option
if option == "1":
print("You step inside the ominous and dank dungeon.")
time.sleep (3)
print("as you enter the dungeon you hear a mysterious whisper in your ear")
time.sleep (3)
print("the voice says that you may not leave until you find the treasure within the central hall of the dungeon.")
time.sleep (3)
print("You turn around as see that the door behind you has magically disappeared.")
time.sleep (3)
print("You begin to look around the room")
time.sleep (3)
print("you see three hallways, one to the left, one to the right and one down the center.")
time.sleep (3)
print("The one on the right looks like it leads you towards what may be the kitchen")
time.sleep (3)
print("the other on the left may lead to the pantry")
time.sleep (3)
print("the very center hallway leads to what looks like the central hall.")
time.sleep (3)
print("Which will you choose?")
if option == "2":
print("You have left the Dungeon of Doom")

If statement issue with basic python program [python]

I a new in programming , was trying some concepts with if else in python
The if else statement is not working as it should.
I'm using nested if-else , however only included the basic code in the code block
I am using a string as an input and then comparing the input with if else statements.
I tried the code in Thonny ide and it works when I debug the program , but after trying to run the program it does not print anything . Alternatively if I use an else statement instead of the elif in the end , only the code in the else statement will print
the code is :
new_value = input("enter your choice between left and right")
if new_value =='left':
print("You chose left")
elif new_value =="right":
print("you chose right")
This code is correct.
new_value = input("enter your choice between left and right")
if new_value =="left":
print("You chose left")
elif new_value =="right":
print("you chose right")
Alternatively if you use an else statement reffer it,
if new_value =='left':
print("You chose left")
else:
print("you chose right")
provide your full nested loop so i will understand problem.

How to have a function return a variable that can be used in the rest of the program? Python 3

I'm trying to make a name generator function which will then return the parts of the name for use in the rest of my program. I put this inside of a function so that I could give the user the option to regenerate the name, instead of being stuck with the first one.
Putting the lists inside the function, and then being able to call the function again causes the random numbers to recalculate, so a new name is generated, but when I try to use "return" to move the variables outside of the function, I get an error.
There's probably a better way to do this, but I'm drawing a blank.
Here's what I have:
def naming():
titles = ["Sir", "Dr.", "Reverend", "Madam", "Master", 'Miss', 'Mrs.']
descriptors = ['sane', 'feelbe', 'cross-eyed', 'bow-legged', 'mad man', 'strange', 'frail', 'old', 'insane', 'cruel', 'bonkers', 'big-headed', 'knock-kneed', 'esquire', 'the huge']
name = input("What is your name?\n> ")
title = titles[random.randrange(0, 6)]
descriptor = descriptors[random.randrange(0,14)]
print(f"You shall be called {title} {name} the {descriptor}")
print("Does that work for you?")
choice = input("> ")
if choice == "yes":
return title, name, descriptor
if choice == "no":
print("Sorry, I'll try again.")
naming()
else:
print("Sorry I don't understand.")
naming()
I actually figured it out on my own. Here's what I did:
while True:
titles = ["Sir", "Dr.", "Reverend", "Madam", "Master", 'Miss', 'Mrs.']
descriptors = ['sane', 'feelbe', 'cross-eyed', 'bow-legged', 'mad man', 'strange', 'frail', 'old', 'insane', 'cruel', 'bonkers', 'big-headed', 'knock-kneed', 'scissor-handed', 'huge']
name = input("What is your name?\n> ")
title = titles[random.randrange(0, 6)]
descriptor = descriptors[random.randrange(0,14)]
print(f"You shall be called {title} {name} the {descriptor}")
print("Does that work for you?")
choice = input("> ")
if choice == "yes":
break
if choice == "no":
print("Sorry, I'll try again.")
else:
print("Sorry I don't understand.")
The code works fine, have you tried to atribute the return values to variables?
IE:
title, name, descriptor = naming()

how to insert a score counter in python

def quiz(demand,correct):
print(" ")
Score=0
Answer=input(demand)
Answer=Answer.lower()
if Answer!="y" and Answer!="n":
print("I did not understand the answer")
quiz(demand,correct)
elif Answer==correct:
print("correct answer")
Score=Score+1
return Score
else:
print("wrong answer")
demand1="the Napoleon's horse is white? y/n: "
correct1="y"
quiz(demand1,correct1)
demand2="berlusconi is president of italy? y/n: "
correct2="n"
quiz(demand2,correct2)
print("score:",Score)
I'm trying to insert a score counter,
why does not it work?
can someone give me the solution?
I'm sorry for my bad english.
The issue is scope, score gets set to zero every time you call quiz
The quickest solution is as follows
Score=0
def quiz(demand,correct):
print(" ")
<everything else is the same>
Call the function and assign the value to a variable and print. Note that the variable scope is local to a function and calling it from outside requires some special declaration global.
Score = 0
def quiz(demand,correct):
global Score

yes or no output in python

I am new to python I am trying to code this, I am asking a question, hence the "Are you a mutant" and depending on if the user responds with a yes or no it should come up the respective output but it works only for yes but not for no. how do i make it work for the elif output?
print("Are you a mutant?")
answer = input()
if 'Yes':
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif 'No':
print("Your application was not successful on this occassion")
`
You need to compare the variable that stores the users input with the thing you are comparing it to. In this case using the ==. Below is revised code off your example:
print("Are you a mutant?")
answer = input()
if answer == 'Yes':
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == 'No':
print("Your application was not successful on this occassion")
You have to write raw_input instead of input. 'Input' just takes the text value but 'raw_input'get the input as a string.
If you are using python2, then follow the code below:
print("Are you a mutant?")
answer = raw_input("Yes/No: ")
if answer == "Yes":
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
print("Your application was not successful on this occasion")
In python3 raw_input() was renamed to input(). Then follow the code below:
print("Are you a mutant?")
answer = input("Yes/No: ")
if answer == "Yes":
print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
print("Your application was not successful on this occasion")

Resources