Weird Bug in Python - python

I've been recently trying to make a bot in Python but got some weird bugs. When I run my code, I get a weird output. Here's my code:
from random import randint
import random
welcome_message = 'Hello, I\'m Vister, your personal assistant. You can command me to do almost anything a computer can do. So, shall we get started? (yes/no)\n'
user_name_message = 'OK, what should I call you?\n'
setup_complete_message = 'That\'s it for setup!'
prompt_message = 'So, what shall I do for you today?\n'
jokes = ['Why did the student eat his homework? Because his teacher told him that it would be a piece of cake!', 'How do you stop an astronaut’s baby from crying? You rocket!', 'Why was 6 afraid of 7? Because 7, 8, 9.', 'Why did the cow cross the road? Because it wanted to go go to the Moovies!']
def get_user_name(message):
user_name = input(message)
print(f'OK, {user_name}! {setup_complete_message}')
def start(message):
welcome_message_input = input(message)
if welcome_message_input == 'yes':
get_user_name(user_name_message)
elif welcome_message_input == 'no':
print('OK, Exiting Applcation...')
else:
print('Invalid Command. Try "yes" or "no".')
def prompt(message):
command = input(message)
if command == 'tell me a joke':
print(jokes[randint(-1, 3)])
elif command == 'give me a random number':
print(random.randint(0, 1000000))
elif command == 'give me a random number in decimals':
print(random.random())
else:
print('Invalid command. Try "tell me a joke" or "give me a random number".')
start(welcome_message)
prompt(prompt_message)
I can only type one command, and then the program exits. That's not what I want for my program, so can somebody help?

It's because you're calling input multiple times. Each input only looks for one thing. If it doesn't match, it does nothing and the next line asks for more input. Try this:
def prompt(message):
command = input(message)
if command == 'tell me a joke':
print(jokes[randint(-1, 3)])
elif command == 'give me a random number':
print(randint(0, 1000000000))
elif command == 'give me a random number with decimals':
print(random(0, 1000000000))
else:
print('Invalid command. Try "tell me a joke" or "give me a random number".')

Related

Can't end program with break. Break is used inside defined function and function is later used inside while loop. What can I do?

I need a help with a certain situation.
We have the following code:
import random
Genre = ["Power metal", "Melodic death metal", "Progressive metal", "Rock"]
Power_metal = ["Helloween", "Dragonforce", "Hammerfall"]
Melodic_death_metal = ["Kalmah", "Insomnium", "Ensiferum", "Wintersun"]
Progressive_metal = ["Dream theater", "Symphony X"]
Rock = ["Bon Jovi", "Nirvana"]
a = ["Pumpkins united", "Halloween"]
Helloween = set(a)
Music = input("\nSelect a music genre from available: %s:" % (Genre))
def bands(Music):
while True:
if Music == "Melodic death metal":
return (Melodic_death_metal)
elif Music == "Power metal":
return (Power_metal)
elif Music == "Progressive metal":
return (Progressive_metal)
elif Music == "Rock":
return (Rock)
else:
Music = input("\nYou entered the wrong name. Please, enter again: %s:" % (Genre))
Band = input("\nReffering to chosen genre, I have bands such as: %s. Choose a specific band and I'll suggest a random song:\n" % bands(Music))
def fate(Band):
while True:
if Band == "Helloween":
if len(Helloween) != 0:
print("Suggested song: ", Helloween.pop())
break
elif len(Helloween) == 0:
print("I don't have more songs to propose. Enjoy your music")
break
fate(Band)
while True:
next = input("\nWould you like me to suggest one more song from the band of your choice ? Answer yes or no:\n")
if next == "no":
print("Enjoy your music")
break
elif next == "yes":
fate(Band)
The problem is: when we get to the point where the program has no more songs to offer, it should print: "I don't have more songs to propose. Enjoy your music"and break. But instead of this, it goes all the way to the last loop and doesn't end until I type in the console no.
Can I make somehow the statement inside loop to end program even if I type in console yes?
Cause it will keep printing:
I don't have more songs to propose. Enjoy your music.
Would you like me to suggest one more song from the band of your choice ? Answer yes or no:
Thanks for the help.

How to handle user input in Python?

I'm making a small game.
Now I need to figure out how to handle users input.
A user must be able to take or drop items.
Stating: 'TAKE item'.
The 'item' is in a list, so it can be anything.
command = input("> ")
command = command.upper()
if self.current_room.is_connected(command):
self.move(command)
elif command in ['HELP', 'LOOK', 'QUIT', 'INVENTORY']:
self.commands(command)
elif ??? TAKE 'item'
self.take(command)
else:
print("Invalid move!")
How do i have this code be transferred to the take method.
And how do i split those 2 word inputs?
def take(self, command):
if command == 'TAKE':
if 'item' not in self.current_room.inventory:
print('No such item.')
else:
self.current_room.inventory.remove_item('item')
self.player_inventory.add_item('item')
print('item', 'taken.')
Currently, your commands don't split the command arguments in to an arg list. This is one fairly simple way to achieve what you're looking for, by splitting into arguments before you begin your parse. This is part of a process called tokenizing in the realm of compilers.
command = input("> ")
command = command.upper().split()
if self.current_room.is_connected(command[0]):
self.move(command[0])
elif command[0] in ['HELP', 'LOOK', 'QUIT', 'INVENTORY']:
self.commands(command)
elif command[0] in ['TAKE', 'DROP']:
item = command[1]
if command[0] == 'TAKE':
self.take(command[1])
else:
self.drop(command[1])
else:
print("Invalid move!")
You should be able to fairly easily handle errors in the take and drop methods. Here are a few cases you may want to check:
displaying a message if you're not carrying the item ( when you drop it)
if the item doesn't exist in the room (when you take it)
if you're already carrying too much (when you take it)

Recursion with defined functions for simple text-based Python program

I'm writing my first program - it's an idiom generator, which combines individual elements from lists of random verbs, nouns, and pronouns (that I have entered) in Madlibs style and generates a humorous expression. This is a simplified version of my source code:
baseFunction = True
def mainFunction() :
import random
quest = input("Which language do you want it in? Type 'French' or 'English'. ")
if quest == "French" or "french":
verb =
#list of verbs I have manually entered
noun =
#list of nouns I have manually entered
pronoun =
#list of pronouns I have manually entered
morenouns =
#list of nouns I have manually entered
phrase = random.choice(verb) + random.choice(noun) + random.choice(pronoun) + random.choice(morenouns)
print(phrase)
print("Now, give it some meaning and use in the world!")
elif quest == "English" or "english":
verb =
#another list of verbs I have manually entered
noun =
#another list of nouns I have manually entered
pronoun =
#another list of pronouns I have manually entered
morenouns =
#another list of nouns I have manually entered
phrase = random.choice(verb) + random.choice(noun) + random.choice(pronoun) + random.choice(morenouns)
print(phrase)
print("Now, invent some meaning for it and use it in the world!")
f8 = input("Do you want to make another one? Say 'yes' if you do. ")
if f8 == "yes" or "Yes":
mainFunction()
else:
print("Thanks for playing!")
else:
print("Didn't quite catch that. Try again! (say yes!)")
mainFunction()
def malif() :
ques = input("Want to hear a funny idiom? Say 'yes' or 'no'. ")
if ques == "yes" or "Yes":
mainFunction()
elif ques == "no" or "No":
print("Wrong answer. Try again! (say yes)")
malif()
else:
print("Didn't quite catch that. Say 'yes' or 'no'.")
while baseFunction :
malif()
mainFunction()
Essentially, I am asking the user whether they want to make an idiom, offering them a choice of language, generating the expression for them, and then asking them if they want to repeat the process. When I run the script in PyCharm, it runs the two functions in order (meaning, malif() first and then mainFunction(), as I have it at the end) but it does not pay any attention to my input (ex. if I say 'no' it runs the mainFunction anyway and will always do it in French even if I say 'English').
I used some of the tips discussed in this entry (Python - How to make program go back to the top of the code instead of closing). I think the problem lies calling the functions in their own definitions (ex. calling malif() if I answer 'no' to input 'ques', which is defined in malif() ). Yet, I have followed the tips discussed in the question that I linked and it is still not working the way that I want it to. Am I doing something wrong in formatting the code (ex. in terms of indentation) or if it is not obvious what I am doing wrong, is there a way for me to loop functions back to the beginning that was not suggested in the original question?
Thanks!
First some tips when you work with strings as input. Python will make the difference between caps and non-caps letter, thus a good way to deal with strings is to lower() them first (or upper(), ...):
Example:
ques = input("Enter Yes or No: ")
ques = ques.lower()
if ques == "yes":
# do something
elif ques == "no":
# do something else
else:
# raise error
Now I feel like your code is build in a funny way. A good habit is to separate the import from the functions, from the main program. The 2 first will be imported if the module (file) is imported, while the last one will be played when the file is executed. To do so, you can use this:
# -*- coding: utf-8 -*-
"""
docstring of the module
"""
# Imports
import random
import os
# Functions
def f():
return "Hello world"
# Main program
if __name__ == '__main__':
# Calling the function, taking the inputs and so on
In the main program, it's rather useful to deal with the possibility that an exception is raised. Moreover, if you use the cmd to display your program, the cmd will close immediately when an error is raised. This syntax is quite useful:
if __name__ == '__main__':
try:
# Do stuff
except:
import sys
print (sys.exc_info()[0])
import traceback
print (traceback.format_exc())
os.system("pause") # for windows, else easy way is to have an empty input to freeze the cmd
Now your code. I would rework it this way:
# -*- coding: utf-8 -*-
"""
Docstring
"""
# Imports
import random
import os
# Functions
def build_a_phrase(language) :
if language == "french":
verb = ["vendre", "atterir", "attaquer", "jeter"]
#list of verbs I have manually entered
noun = ["arbre", "poisson", "chien"]
#list of nouns I have manually entered
pronoun = ["un", "les"]
#list of pronouns I have manually entered
morenouns = ["chat", "oiseau"]
#list of nouns I have manually entered
choices = [random.choice(verb), random.choice(noun), random.choice(pronoun), random.choice(morenouns)]
phrase = " ".join(choices) # Add a space between the words
return phrase
elif language == "english":
verb = ["...", "...", "..."]
#another list of verbs I have manually entered
noun = ["...", "...", "..."]
#another list of nouns I have manually entered
pronoun = ["...", "...", "..."]
#another list of pronouns I have manually entered
morenouns = ["...", "...", "..."]
#another list of nouns I have manually entered
choices = [random.choice(verb), random.choice(noun), random.choice(pronoun), random.choice(morenouns)]
phrase = " ".join(choices) # Add a space between the words
return phrase
if __name__ == '__main__':
try:
# Parameters
available_language = ["french", "english"]
available_answers = ["yes", "no"]
# Safety implementation of an input
quest = ""
i = 0
while quest.lower() not in available_answers:
quest = input("Want to hear a funny idiom? Say 'yes' or 'no'. ")
i += 1
if i == 2: # number of tries
break
if quest.lower() == "no":
print ("I'm sure you meant yes.")
language = ""
i = 0
while language.lower() not in available_language:
language = input("Which language do you want it in? Type 'French' or 'English'.\n")
i += 1
if i == 2: # number of tries
break
while True:
sentence = build_a_phrase(language)
print (sentence)
print ("Now, give it some meaning and use in the world!")
f8 = ""
i = 0
while f8.lower() not in available_answers:
f8 = input("Do you want to make another one? Say 'yes' if you do. ")
i += 1
if i == 2: # number of tries
break
if f8.lower() == "no":
print("Thanks for playing!")
break
except:
import sys
print (sys.exc_info()[0])
import traceback
print (traceback.format_exc())
os.system("pause")
Hope you'll get a few good tricks from this answer, and some good habits :)
Not complete yet, when the input is wrong, an Error should be raised rather than waiting for the error resulting in the wrong input (i.e. a raise statement should be placed instead of the breaks)

How to use a function's if statement to use info from another function?

So I'm designing a sign-in AI, and I want it to work so that the admin name is Shawn. Here is my issue:
The program starts with the interface -
def interface():
username = input('Hello traveler, what is your name? ')
lowerUsername = username.lower()
print()
print()
if lowerUsername == 'megan':
print()
print('So you are the one Shawn has told me so much about? You are looking beautiful today my dear ☺️🌷')
elif lowerUsername == 'shawn':
OfficialSignInEdit()
So you can see at the end that if the user inputs that their name is 'shawn' at sign-in, it calls on the OfficialSignInEdit function, which is the admin sign in. It looks like this:
def OfficialSignInEdit():
print()
if PossInputs('perio', 'What street did you grow up on?: ') == correct:
print()
print('Greetings Master Shawn, it is a pleasure to see you again 🙂')
else:
print()
res1 = input('Incorrect password, try again? (Yes/No)')
lowres1 = res1.lower()
if lowres1 == 'yes':
print()
print()
OfficialSignIn()
elif lowres1 == 'no':
print()
print()
interface()
So I have pinpointed the source of my issue to be right here in this particular line:
if PossInputs('perio', 'What street did you grow up on?: ') == correct:
print()
print('Greetings Master Shawn, it is a pleasure to see you again 🙂')
this (just for your reference) is the PossInputs function:
def PossInputs(x, y):
term = x
question = input(y)
lowQuestion = question.lower()
words = lowQuestion.split()
if term in words:
print()
print (correct)
else:
print()
print (incorrect)
So what I want to happen is, when 'shawn' is entered as a name, the program will jump to the OfficialSignInEdit Function, and ask the question 'What street did you grow up on?'. Then IF the user enters the answer 'perio', the program will print 'correct', and then print the message 'Greetings Master Shawn, it is a pleasure to see you again'. I tried to say that IF PossInputs == correct (and I did define correct = 'correct', and incorrect = 'incorrect' outside all functions) then this would happen, but instead it prints 'correct', and then 'Incorrect password, try again? (Yes/No)', so how can I make a conditional statement that says that if the user answers 'perio', then it will print the welcome message?
Just for thoroughness sake, I also tried
if PossInputs('perio', 'What street did you grow up on?: ') == True
also without success...
anyways anything you can give me is extremely appreciated, if you have any questions or you would like to to clarify something about the written code, I would be more than happy to get back with you as soon as I can.
Thanks!

Nosetests - Guess The Number Game

I am trying to write a few tests for a random input number game but not too sure how to proceed on.
I am following the Python Game from http://inventwithpython.com/chapter4.html
Started the tests with a file test_guess.py
from unittest import TestCase
import pexpect as pe
import guess as g
class GuessTest(TestCase):
def setUp(self):
self.intro = 'I have chosen a number from 1-10'
self.request = 'Guess a number: '
self.responseHigh = "That's too high."
self.responseLow = "That's too low."
self.responseCorrect = "That's right!"
self.goodbye = 'Goodbye and thanks for playing!'
def test_main(self):
#cannot execute main now because it will
#require user input
from guess import main
def test_guessing_hi_low_4(self):
# Conversation assuming number is 4
child = pe.spawn('python guess.py')
child.expect(self.intro,timeout=5)
child.expect(self.request,timeout=5)
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
child.sendline('3')
child.expect(self.responseLow,timeout=5)
child.sendline('4')
child.expect(self.responseCorrect,timeout=5)
child.expect(self.goodbye,timeout=5)
def test_guessing_low_hi_4(self):
# Conversation assuming number is 4
child = pe.spawn('python guess.py')
child.expect(self.intro,timeout=5)
child.expect(self.request,timeout=5)
child.sendline('3')
child.expect(self.responseLow,timeout=5)
child.sendline('5')
child.expect(self.responseHigh,timeout=5)
child.sendline('4')
child.expect(self.responseCorrect,timeout=5)
child.expect(self.goodbye,timeout=5)
and the guess.py file with
intro = 'I have chosen a number from 1-10'
request = 'Guess a number: '
responseHigh = "That's too high."
responseLow = "That's too low."
responseCorrect = "That's right!"
goodbye = 'Goodbye and thanks for playing!'
def main():
print(intro)
user_input = raw_input(request)
print(responseHigh)
print(request)
user_input = raw_input(request)
print(responseLow)
user_input = raw_input(request)
print(responseCorrect)
print(goodbye)
if __name__ == '__main__':
main()
Not sure How to proceed on with writing a few more tests with if statement to test if the value is low or high. I was told to try a command line switch like optparse to pass the number but not sure how to do that either.
Somewhat of a new person with Python, any guidance or assistance would be appreciated.
In order to do command line parsing in nosetests, you'll have to do something similar to this (at least that's what I had to do), i.e. create a plugin that will give you access to the command line parameter in nosetests. Once you had the plugin added which gave you the command line parameter, it would be pretty easy to create a test that would take advantage of that passed in parameter.
from test_args import case_options
class GuessTest(TestCase):
...
def test_guessing(self):
# Conversation assuming number is 4
if case_options.number < 4:
# Do something
elif case_option.number > 4:
# Do some other test
else:
# Do the final test
Does that make sense? I might be misunderstanding what you are trying to do so if I am, just let me know and hopefully we can clear it up.

Categories