Test case for the magic-8-ball game [closed] - python

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 4 years ago.
Improve this question
I'm mew to python and to TDD in general, I have developed a magic-8-ball game and I would like to learn how I can write the Test case for this program. The test should ensure the following:
Allow the user to input their question
Show an in progress message
Create 10/20 responses and show a random response
Allow the user to ask another question/advice or quit the game.
Below is my code. I know I should write Tests first but like I said, this a new territory for me.
RESPONSES = ("It is certain", "It is decidedly so", "Without a doubt", "Yes-definitely",
"You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Yes",
"Signs point to yes", "Reply is hazy", "Ask again later", "Better not tell you now",
"Cannot predict now", "Concentrate and ask again", "Don't count on it",
" My reply is no", "My sources say no", "Outlook not so good", "Very doubtful")
from time import sleep
from random import choice
class MagicBall:
def input_question(self):
play_again = 'yes'
while play_again == 'yes':
str(input('Enter your question: '))
for i in range(3):
print("Loading {}".format(".."*i))
sleep(1)
print(choice(RESPONSES))
play_again = str(input("Would you like to ask another question? yes/no ")).lower()
if play_again == 'no':
print("Goodbye! Thanks for playing!")
SystemExit()
magic = MagicBall()
magic.input_question()

Unit tests are written to confirm that the expected output is obtained from a range of inputs for any function or method that carries out some computations and returns a value.
If you had a function to calculate the sum of two number:
def calc(first,second):
return first + second
To confirm that the correct result is obtained you would do the following:
self.assertEqual(calc(5,5), 10)
You expect 10 to be the result from 5 and 5 so if it is something else it will produce an error.
I have also noticed this is a question from the Andela Interview questions scheduled for next week. Please review the documentation you were given for more clarity on how to write tests for different cases.

Related

Python - How do I get this to read the code? [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 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!")

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.

Indent placement? [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 8 years ago.
Improve this question
I've tried to design a small (3 rooms) "adventure-like" mini-game, and when i run it i get:
if answer == "restaurant":
IndentationError: unindent does not match any outer indentation level
# TRUMANIA v. 0.0.2
def Square():
print "You exit from there, and you appear in Central Square. Do you want to walk to the Library or to the Restaurant?"
answer = raw_input("Type 'Restaurant' or 'Library' and hit 'Enter'.").lower()
if answer == "restaurant":
Restaurant()
elif answer == "library":
Library()
else:
print "You didn't pick Restaurant or Library! You can't just stand there like a rock; you need to make a decision!"
Square()
def Trumania():
print "You've just entered Trumania!"
print "Do you want to go to the Restaurant or visit the Library?"
answer = raw_input("Type 'Restaurant' or 'Library' and hit 'Enter'.").lower()
if answer == "restaurant":
Restaurant()
elif answer == "library":
Library()
else:
print "You didn't pick Restaurant or Library! You can't just stand in the entrance like a scarecrow; you need to make a decision!"
Trumania()
def Restaurant():
print "You've just entered the Restaurant!"
print "Do you want to eat Inside or Outside?"
answer = raw_input("Type 'Inside' or 'Outside' and hit 'Enter'.").lower()
if answer == "inside":
print "You need to bribe the waiter, but you get a cozy table and have a wonderful meal!"
elif answer == "outside":
print "You get a cheap (and cold) table in the outside and manage to eat some apetizers"
else:
print "You didn't selected Inside or Outside. The Waiter euh... Waits... "
Restaurant()
def Library():
print "You arrive at the Library!"
print "Do you want to get some books or you prefer to study?"
answer = raw_input("Type 'Books' or 'Study' and hit 'Enter'.").lower()
if answer == "books":
print "You get some books that you can sell later. However, thats bad karma!"
elif answer == "study":
print "You learn a lot!However, you feel tired after so much effort"
else:
print "You didn't pick Books or Study! Try again."
Library()
Trumania()
Square()
The idea is to start in Trumannia, then be able to choose Restaurant or Library, and then exit to Square and be able to choose again Restaurant or Library. So I'm not sure a) how to fix the error and b) how to format the different variables so the program can "load" all the info first and then "go" to each place when needed. I hope i explained myself. Thanks again!
Your code is broken because there is no colon after def Square().

Python programming error, elif statment syntax error Simple code [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 8 years ago.
Improve this question
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
Whats wrong with this code?? It says in the idle shell syntax error. The last elif statement isn't working.
This is a petty indentation issue, your print statements for the if blocks are not indented right and so the elif seems to be out of place. Note that python keeps track of logical blocks by the indentation.
name=input("Hello person, Whats your name?")
print("Hello", name)
print("Do you want to hear a story?", name)
choice=input("Yes, No?")
if choice==("yes" or "yes " or "Yes" or "Yes "):
print("Ok", name,", listen up")
print("There was once an old, old house at the top of a hill Sooooo high it was above the clouds")
choice2=input("What do you want to call the house?")
print("The old,",choice2,"was once owned by an old lady. ")
elif choice==("maybe"):
print("You found an easter egg, congrats. PS this does nothing")
As already pointed out, if choice==("yes" or "yes " or "Yes" or "Yes ") is wrong, use if choice.lower().strip() == "yes" instead, or if choice in ("yes", "yes ", "Yes", "Yes ").
If in case this is python 2, input will throw an error, use raw_input instead.
Also print with multiple statements will throw errors as well if used like a function, so change them from print(statement_x, statement_y, statement_z) to print statement_x, statement_y, statement_z

How to create a menu or directory? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
How do I make a simple Menu/Directory using Python? I would like to have letters that the user would press to do tasks, and when they enter the letter after the prompt, the task is done... for example:
A. Create Username
B. Edit Username
C. Exit
Choice:
And then all the user has to do is enter one of the letters after the prompt.
A (very) basic approach would be something like this:
print "A. Create Username"
print "B. Edit Username"
input = raw_input("Enter your choice")
if input == "A":
print "A was given"
if input == "B":
print "B was given"
A very basic version:
def foo():
print "Creating username..."
def bar():
print "Editing username..."
while True:
print "A. Create Username"
print "B. Edit Username"
print "C. Exit"
choice = raw_input()
if choice.lower() == 'a':
foo()
elif choice.lower() == 'b':
bar()
elif choice.lower() == 'c':
break
else:
print "Invalid choice"
Accepts upper- and lower-case letters as choice.
Console Menu Generator in Python
Have a read and post back with your efforts as stated by dm03514

Categories

Resources