Start() is not defined - python

Hey I am trying to create a simple text based slot machine with the view to convert it into a graphical one latter.
I have started by it prompting a menu which works fine. However when the user enters the required 'p' to continue it won't call the next function because I haven't defined it yet.... I have?
from time import sleep
from random import shuffle
#Creates the class
class Machine():
#This is the constructor full of attributes
def __init__(self):
self.reel1 = ["Lemon", "Bell", "Cherry"]
self.reel2 = ["Lemon", "Bell", "Cherry"]
self.reel3 = ["Lemon", "Bell", "Cherry"]
firstSlide = self.reel1
secondSlide = self.reel2
thirdSlide = self.reel3
self.currentFunds = "10"
funds = self.currentFunds
f = open('score.txt', 'w')
f.write(funds)
#Dictates all the funds and checks if the user has enough money or needs to add money
def Funds(self):
if self.currentFunds == "0":
print("You are out of credits! :( \n")
Menu()
#Starts the spinning and randomizes the lists
def Start(self, firstSlide, secondSlide, thirdSlide):
shuffle(firstSlide, secondSlide, thirdSlide)
print(firstSlide[0], secondSlide[1], thirdSlide[3])
#Intro Menu to give player stats and options
def Menu(self):
play = ""
m = Machine()
print('*****************\n')
print(' WELCOME! \n')
print('*****************\n')
print('Current Credits: ', m.currentFunds)
if input("Press P to play \n") == "P" or "p":
machine = Start()
machine.Start()
machine = Machine()
while True:
machine.Menu()
Any ideas?

You have Start as a member function of the Machine class. You need to replace machine = Start() with self.Start().
It actually looks like this is the case with a number of the variables you seem to be trying to use. For example, I would expect that Start would rely on self.start, but it is relying on parameters (which you are not passing in).
As a general comment on this code, I'm wondering if you really need/want to have this be structured this way. You seem to be creating the object recursively and I think you might be better off restructuring a bit.

Related

How do I Run a Background Timer for Each Prompt in My Word Game?

I'm making an unoriginal game for a first project that just runs in my python terminal. The user is randomly given a set of 2-3 letters and the user has to come up with a real word (checked by the Webster dictionary) that contains the given set of letters within 5 seconds. For example, if the game generates "le" the user can input "elephant" within 5 seconds as a correct word and gives them a point.
The problem is that I can't seem to implement the 5 second timer function to run in the back for every time a prompt is given without messing up some other part or running into another problem. I've looked into threading and can't seem to make use of it.
Here is the code for the main game:
from letters import letter_sets_list
fhand = open("words_dictionary.json")
data = fhand.read()
global score
score = int(0)
game_over = False
while game_over is False:
import random
random_letters = random.choice(letter_sets_list)
print('Word that contains:', random_letters)
answer = input("Type a word:")
if answer in data and random_letters in answer:
score += 1
print("nice one")
else:
game_over = True
print("Game Over \n Score:", score)
fhand.close()
exit()
Here is the timer function I found off YouTube and tried to implement:
def countdown():
global my_timer
my_timer = int(5)
for x in range(5):
my_timer -= 1
sleep(1)
countdown_thread = threading.Thread(target=countdown)
countdown_thread.start()
Take a look at that. Especially check if that will work for you:
import time
from threading import Thread
answer = None
def check():
time.sleep(2)
if answer != None:
return
print("Too Slow")
Thread(target = check).start()
answer = input("Input something: ")
Edit: I tried to implement code from my previous answer to your code but with a little different approach:
import time, threading
#from letters import letter_sets_list
#import random
#fhand = open("words_dictionary.json")
#data = fhand.read()
data = ["answer"]
answer = [None]
random_letters = [None]
global score
score = int(0)
game_over = False
x = 0
def game(answer, random_letters):
#random_letters = random.choice(letter_sets_list)
print('Word that contains:', random_letters)
while True:
answer[0] = input("Type a word: ")
mythread = threading.Thread(target=game, args=(answer, random_letters))
mythread.daemon = True
mythread.start()
for increment in range(5):
time.sleep(1)
if answer[0] in data: # and random_letters in answer
score += 1
print("Good answer")
x = 1
break
if x != 1:
print("\nTime is up")
else:
x = 0
game_over = True
In this approach I didnt use time.sleep() inside threaded function but outside of it which helps with killing it. Also i assumed that if your answer is incorrect you would like to have another chance to answer untill time is up or answer is correct so I just used while loop in threaded function. The code is quite simple so I think if you spend a little time analysing your parts of the code you will figure it out.
Some parts of the code I didn't use as I dont have access to your json files ect., but if I understand correctly what you're trying to do, it shoud work. Also check how will behave your kernel. In my case sometimes it shows some problems but my PC is not the best and so it might be only problem with RAM or other part of my computer as it happens quite often with my projects.

Class import from another .py file

This is my first question here so please don't roast me if it seems obvious or stupid ;)
I've started learning Python two months ago and I'm trying to get my head wrapped around some basic functions of this language.
I've created a script that creates a basic blockchain and generates a QR code at the end. I've created the following class:
class BlobChang:
def __init__(self, string=None, rand=None, conc=None, ha=None, blobchang=''):
self.string = string
self.rand = rand
self.conc = conc
self.ha = ha
self.blobchang = blobchang
def input(self):
self.string = input('Input: ')
def h(self):
self.rand = str(random.randrange(1, 1000))
self.conc = self.string + self.rand
self.ha = hashlib.sha256(self.string.encode('utf-8')).hexdigest()
self.blobchang = self.blobchang + self.ha
print(self.blobchang)
I've named that script BlobChang.py. In the same directory I've created a new Python file import_test.py and I've imported the BlobChang class:
from BlobChang import BlobChang
I've created an instance of this class named test:
test = BlobChang()
and it worked fine. I did test.input() and it worked, then test.h() and this worked as well but then something weird happened. The script instead of finishing started executing the remainder of the original code from BlobChang.py! It looks like this:
check_continue = input('Want to create a BlobChang? (Y/N): \n')
while if_yes(check_continue):
x.input()
x.h()
check = input('Want to continue? (Y/N): \n')
check_qr = input('Do you want to create a QR Code? (Y/N): \n')
if if_yes(check_qr) == True:
filename = str(input("Enter file name: "))+".png"
qrcode.make(x.blobchang).save(filename)
print("Your file name is: ", filename)
print('Thank you for working with BlobChang!')
However, it stopped only at the first conditional statement and went into a loop. Why did that happen?
The variable check_continue is never updated that's why the condition in while if_yes(check_continue): always remains true and the code goes into an infinite loop, maybe you want check_continue = input('Want to continue? (Y/N): \n') in line 5

What kind of unittests make sense for a chatbot in python?

I am creating a simple chatbot at the moment and now I would like to test it using the unittests package. I am new to programming and I have only recently heard about the existence of unit tests. I am thinking that for a chatbot to be tested, it would make sense to see if it gives the right output to the right user input. So if the user writes "Hi", for example, it should output "Hey what's up?" as specified in the library (I have written a bunch of libraries for it; it is not a deep-learning-kinda-bot).
This is the code of my chatbot:
# importing regular expression operations, which we need later to access the libraries
import re
import random # later we want to pick a chatbot answer to a statement by random
from Jordan_Library import dict_smalltalk, dict_caring, dict_cursing, dict_meditating, dict_corona # import libraries
# and here we imported all the topic libraries
class JordanBot:
"""
A class that represents the abilities of Jordan the chatbot.
...
Attributes
----------
name : str
user can insert name, which will later be shown in the chat.
Methods
----------
name : str
user can insert name, which will later be shown in the chat.
"""
def __init__(self, name): # this function is always called when a new object of the class is called
"""
Constructing topics for Jordan. Takes the user's name as an argument.
"""
self.name = name
def chat(self):
"""
A function that enables chatting with Jordan after picking a topic.
Take no arguments.
"""
topics = [dict_smalltalk, dict_caring, dict_cursing, dict_meditating, dict_corona]
while True:
print("Welcome. My name is Jordan. You can now choose a topic that we can talk about.")
print("Press '0' to have some good old-fashioned smalltalk.")
print("Press '1' to tell me about your deepest sorrows and your destroyed soul.")
print("Press '2' to curse with me.")
print("Press '3' to meditate with me.")
print("Press '4' to talk about Corona.")
# execute this welcome text and tell user what to press in order to pick a topic.
choice = input(">>> ")
# initialize input
if choice == '0': # determine which key to press to initiate the specific topic
print("Alrighty, let's do some chitchatting.")# initiate welcome text for specific topic
print("Don't forget that I am sensitive to punctuation.")
elif choice == '1':
print("Please tell me about your deepest sorrows and your destroyed soul.")
print("Don't forget that I am sensitive to punctuation.")
elif choice == '2':
print("Make yourself ready. let's insult each other!")
print("Don't forget that I am sensitive to punctuation.")
elif choice == '3':
print("Ok then, let's meditate.")
print("Don't forget that I am sensitive to punctuation..")
elif choice == '4':
print("Let's talk about Corona.")
print("Don't forget that I am sensitive to punctuation..")
elif choice == 'q': # if user wants to quit
break
else: # if user pressed the wrong key.
print("Try again.")
edition = topics[int(choice)]
statement = list(map(lambda x:re.compile(x[0], re.IGNORECASE), edition))
# list(map()) applies a function to all elements of a specified object, in this case the cursing library
# lambda makes sure that re.compile is applied in a certain way to all elements of the library without being case-sensitive
# re.compile makes sure that the elemets are turned into objects that can be matched later to another item in the library
answer = list(map(lambda x:x[1], edition))
# same here, but here we pick the second argument in the list x[1], which entails Jordan's answers
while True:
userInput = input(' ' + self.name + ' >>> ') # this allows for the user to type in keys
resp = "I did not understand what you said. Also, I am sensitive to punctuation." # initalize response variable
counter = 0 # initalize counter
while resp == "I did not understand what you said. Also, I am sensitive to punctuation." and counter < len(statement): # determine when to start my loop
for i in range(0, len(statement)): # loop through the indexed library
match = statement[i].match(userInput) # look if input of the user matches with one of the words in the library
if match:
word = statement[i].split(userInput)[1] # We have to take the first element of this list
resp = random.choice(answer[i]) # if there is a match, pick a random answer from x[1]
counter += 1 # make sure that the counter is now + 1 so it does not write the initialized response from the beginning but continues with the loop
# if there is no match though, then it will write the initialized answer
if userInput == 'q':
print(random.choice(answer[i]))
print("---------------------------------")
print("Do you want to choose another topic? Pick below or press 'q' to quit for realsies.")
print("---------------------------------")
break
resp = resp.format(word)
print('____________')
print(' ')
print('Jordan >>> ' + resp) # print Jordan's answer
The unittests I am trying to create are something like:
import unittest
from Jordan_Library import dict_smalltalk, dict_caring, dict_cursing, dict_meditating, dict_corona # import dictionairies
from Jordan_Class import JordanBot
class testJordan(unittest.TestCase):
"""
A class to test several units of Jordan to make sure everything is working correctly.
"""
def test_match(self):
j = JordanBot('StringName')
j.chat()
user_input = "What are you?"
bot_output = list(map(lambda x:x[1], dict_smalltalk)) # this is how I access the right response in the right library
matched = bot_output.match(user_input)
self.assertIn("I am a chatbot, dude. What do you think?", matched)
def test_regex(self):
j = JordanBot('StringName')
j.chat()
text = 'something'
regex = {}
self.assertRegexpMatches(text, regex)
if __name__ == '__main__':
unittest.main()
The main problem is that these lines only run my code, but not really any unit tests. Advice is much appreciated!

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.

python: using a class to keep track of data used by another class

I'm learning Python via book and internet. I'm trying to keep score of a game in a separate class. In order to test my idea, i've constructed a simple example. It looks too complicated for some reason. Is there a simpler/better/more Pythonic way to do this?
My code is as follows:
import os
class FOO():
def __init__(self):
pass
def account(self, begin, change):
end = float(begin) + float(change)
return (change, end)
class GAME():
def __init_(self):
pass
def play(self, end, game_start):
os.system("clear")
self.foo = FOO()
print "What is the delta?"
change = raw_input('> ')
if game_start == 0:
print "What is the start?"
begin = raw_input('> ')
else:
begin = end
change, end = self.foo.account(begin, change)
print "change = %r" % change
print "end = %r" % end
print "Hit enter to continue."
raw_input('> ')
self.play_again(end, game_start)
def play_again(self, end, game_start):
print "Would you like to play again?"
a = raw_input('> ')
if a == 'yes':
game_start = 1
self.play(end, game_start)
else:
print "no"
exit(0)
game = GAME()
game.play(0, 0)
Here's how I would format your code:
import os
class Game(object):
def play(self, end, game_start=None):
os.system("clear")
change = input('What is the delta? ')
# Shorthand for begin = game_start if game_start else end
begin = game_start or end
end = float(begin + change)
print "change = {}".format(change)
print "end = {}".format(end)
self.play_again(end, game_start)
def play_again(self, end, game_start):
raw_input('Hit enter to continue.')
if raw_input('Would you like to play again? ').lower() in ['yes', 'y']:
self.play(end, game_start)
else:
exit(0)
if __name__ == '__main__':
game = Game()
game.play(0, 0)
And a few tips:
I wouldn't create a new class that contains only code to perform one specific task. If the class doesn't take arguments or doesn't simplify your code, don't create it. Your Game class is an exception, however, as you would probably add more code to it.
In Python, classes are written in CamelCase. Global constants are usually written in UPPERCASE.
raw_input() returns a string. input() returns the string evaluated into a Python object.
I asked the question a better way and got what I was looking for here:
python: how do I call a function without changing an argument?

Categories