I am trying to print the output answer to my GUI in Tkinter and I can not seem to see how it is possible! If anyone has any tips please let me know.
I know its somewhere along the lines of this but I cannot seem to crack it!
DisplayFearFactor.configure( text = " computers FearFactor: " + str(computersCardFearFactor))
DisplayFearFactor.place(bordermode=OUTSIDE, height=60, width=200,x=100,y=100)
here is the full section of code:
def compareFearFactor():
human = []
computer = []
if len(cards) > 0:
human.append(cards.pop(0))
computer.append(cards.pop(0))
humanCard = human.pop(0) if human else None
computersCard = computer.pop(0) if computer else None
print("computers Fear Factor:", computersCard.FearFactor)
Draw = (humanCard.FearFactor == computersCard.FearFactor)
Win = (humanCard.FearFactor > computersCard.FearFactor)
DisplayFearFactor.configure( text = " computers FearFactor: " + str(computersCardFearFactor))
DisplayFearFactor.place(bordermode=OUTSIDE, height=60, width=200,x=100,y=100)
computercardshow = ImageTk.PhotoImage(computersCard.image)
Comp = Label(image = computercardshow)
Comp.configure(image = computercardshow)
Comp.place(x=800,y=50)
Comp.image = computercardshow
if Draw:
print("It's a tie!")
human.append(humanCard)
computer.append(computersCard)
elif Win:
print("You win this hand!")
cards.append(humanCard)
cards.append(computersCard)
playerTurn = True
else:
print("You lose this hand!")
computer.append(computersCard)
computer.append(humanCard)
playerTurn = False
Related
Sometimes it works fine, sometimes throws an error. Cannot figure it out. No matter how many inputs.
Thats the error. Is it the order of the code or something else?
give_gift.remove(reciever)
ValueError: list.remove(x): x not in list
Code:
import random
import os
import time
random.seed()
buy_gift = []
give_gift = []
names = True
print('Add name, then press ENTER. To finish type "end".')
while names is True:
name = input()
if name == "end":
if len(buy_gift) < 2:
print("Go play with your own gift. Goodbye!")
print()
os.system("PAUSE")
quit()
else:
names = False
else:
buy_gift.append(name)
give_gift.append(name)
exchange = len(give_gift)
os.system("cls")
time.sleep(1)
print("Drawing pairs...")
print()
time.sleep(1)
while exchange > 0:
giver = random.choice(buy_gift)
reciever = random.choice(give_gift)
while giver == reciever:
reciever = random.choice(buy_gift)
print(giver,"buys gift for",reciever)
time.sleep(0.25)
buy_gift.remove(giver)
give_gift.remove(reciever)
exchange -= 1
print()
os.system("PAUSE")
There is an issue with this line:
while giver == reciever:
reciever = random.choice(buy_gift)
It should be:
reciever = random.choice(give_gift)
I am fairly new to coding on Python. In my code, I am trying to change the myPlayer.hp and myPlayer.sp values based on what the myPlayer.job is. But, for some reason, regardless of job, the HP and SP values are still 0 when I check them in the program. If you know how to change them, please let me know and thank you.
Here is the code that deals with my question:
class player:
def __init__(self):
self.name = ''
self.job = ''
self.hp = 0
self.sp = 0
self.pwr = 0
self.res = 0
self.agi = 0
self.smr = 0
self.wll = 0
self.status_effects = []
self.location = 'b2'
self.game_over = False
myPlayer = player()
def main_game_loop():
while myPlayer.game_over is False:
prompt()
def setup_game():
os.system('cls')
question1 = "Hello, what's your name?\n"
for character in question1:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
player_name = input("> ")
myPlayer.name = player_name
question2 = "Hello, what role do you want to play?\n"
question2added = "(You can play as a warrior, mage, or priest)\n"
for character in question2:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.05)
for character in question2added:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.01)
player_job = input("> ")
valid_jobs = ['warrior', 'mage', 'priest']
if player_job.lower() in valid_jobs:
myPlayer.job = player_job
print("You are now a " + player_job + "!\n")
while player_job.lower() not in valid_jobs:
player_job = input("> ")
if player_job.lower() in valid_jobs:
myPlayer.job = player_job
print("You are now a " + player_job + "!\n")
if myPlayer.job == 'warrior':
myPlayer.hp = 25
myPlayer.sp = 0
elif myPlayer.job == 'mage':
myPlayer.hp = 15
myPlayer.sp = 20
elif myPlayer.job == 'priest':
myPlayer.hp = 20
myPlayer.sp = 15
Your code isn't necessarily incorrect, but there is a good chance for an error when the player enters their job.
You have the following code:
if player_job.lower() in valid_jobs:
That means that if the player enters Warrior you understand that they meant warrior. That is good. But when you assign the player's job you do the following:
myPlayer.job = player_job
That can cause a problem, since you aren't calling lower() on the job.
Instead, you likely want:
myPlayer.job = player_job.lower()
That way, when you look at the player's job to assign sp and hp it will match the lowercase strings you are comparing it to.
I am trying to make a number guesser in python 2.7 with Tkinter. I am having trouble on making the MessageBox disappear after I hit ok.
import random
import Tkinter
import tkMessageBox
import tkSimpleDialog
root = Tkinter.Tk()
Random_Pick = random.randint(1,10)
No_Tries = 0
G = True
while G == True:
print ('Guess A Number!')
Guess = tkSimpleDialog.askinteger('Game', 'Pick A Number Between 1 And 10:')
print Guess
if Guess is not None:
Guess = int(Guess)
Q = True
if Guess is None:
pass
while Q == True:
if Guess != Random_Pick:
Sorry = tkMessageBox.showinfo ('Sorry', 'You Got It Wrong.')
No_Tries = No_Tries + 1
if Guess == Random_Pick:
No_Tries = str(No_Tries)
if No_Tries == str(1):
OneTry = tkMessageBox.showinfo ('Congratulations!', 'You Got It Right! It took ' + No_Tries + ' Try!' )
print OneTry
else: Tries= tkMessageBox.showinfo ('Congratulations!', 'You Got It Right! It took ' + No_Tries + ' Tries!')
print Tries
G = False
while No_Tries == 3:
Random_Pick = str(Random_Pick)
tkMessageBox.showinfo ('Sorry', 'You Lose. The Number was ' + Random_Pick)
No_Tries = 0
G = False
if AttributeError:
pass
root.mainloop()
I am not sure why the MessageBox Does't disappear after I click ok. Is there anyway to make that happen with Tkinter?
The inner while loop has no exit. It loops on the condition Q == True but nothing inside the loop modifies Q.
I'm writing my first short programs with Python. This program should stop after the third wrong guess. The program works as expected with the first question, and stops itself after three wrong guesses.
However, I cannot understand why, with the second question, after the third wrong guess, the first question is repeated.
easy_text = '''Hello __1__!' In __2__ this is particularly easy; all you
have to do is type in: __3__ "Hello __1__!" Of course, that isn't a very
useful thing to do. However, it is an example of how to output to the user
using the __3__ command, and produces a program which does something, so it
is useful in that capacity..'''
def split_string():
global easy_text_splitted
easy_text_splitted = easy_text.split(" ")
return None
def level1():
print """You will get 3 guesses per problem
The current paragraph reads as such:"""
print easy_text
return first_question(3)
def first_question(guesses):
while guesses > 0:
split_string()
first_answer = raw_input("What should be substituted in for __1__?")
if first_answer.lower() == "world":
easy_text_splitted[1] = first_answer
easy_text_splitted[19] = first_answer
new_easy_text = " ".join(easy_text_splitted)
print new_easy_text
second_question(3)
else:
guesses -= 1
print "That's not the answer I expected. You have " +
str(guesses) + " guess(es) left"
return first_question(guesses)
def second_question(guesses):
while guesses > 0:
split_string()
second_answer = raw_input("What should be substituted in for 2?")
if second_answer.lower() == "python":
easy_text_splitted[4] = second_answer
new_easy_text = " ".join(easy_text_splitted)
print new_easy_text
else:
guesses -= 1
print "That's not the answer I expected. You have \n " +
str(guesses) + " guess(es) left"
return second_question(guesses)
def level_selection(index):
level = raw_input("Choose the desired level of difficulty: easy, \n
medium, or hard ")
if level.lower() == "easy":
return level1()
if level.lower() == "medium":
return level2
if level.lower() == "hard":
return level3
else:
print "Error"
index -= 1
if index > 0:
return level_selection(index)
return "Bye!"
print level_selection(3)
level2 = "You selected medium"
level3 = "You selected hard"
Whatever the value of guesses was in first_question when second_question was called, it is the same when it returns, so the loop continues, and repeats the first question.
Using a debugger will help you follow how this works.
I'm new to programming, and I tried to make a python program. Here it is:
import time
gravity = 0
space = 0
print "Welcome to the 'Find out who is in space' program"
person_messy = raw_input("Enter a name:")
person = person_messy.lower()
if len(person) < 5:
print "Make sure you put the first AND last name"
else:
space = 1
print "Searching database....."
if person == "oleg kotov" or "mike hopkins" or "sergey ryazanskiy" or "fyodor yurchikhin" or "karen nyberg" or "luca permitano":
gravity = 1
else:
gravity = 0
time.sleep(1)
if space == 1:
print "loading..."
time.sleep(1)
if space == 1:
print "loading..."
time.sleep(1)
if space == 1:
print "loading..."
if gravity == 1:
print person + "is in space!"
else:
print "You and " + person + "are on the same planet."
this is the error I got:
Internal error: ReferenceError: _select is not defined
I had the same problem on similar code... You can't use time.sleep or it gives this. I don't know why, but that fixed my code(not using time.sleep)