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.
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 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
I'm trying to make a an input timer on a question that gives the user 5 seconds to answer the question before deciding that no input is the same as the wrong answer, but no body wants to touch it with a 10ft pole, so I'm trying to go about it a different way and break a for loop with input?
Any one got suggestions?
import random
import operator
qLimit = 10
oqLimit = 10
score = 0
maxNum = 10
timeLimit = 0
otimeLimit = 5
import time
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1,maxNum)
y = random.randint(1,maxNum)
op = random.choice(list(ops.keys()))
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion(a):
timeLimit = otimeLimit
for i in range(0,timeLimit):
guess = input("")
print (timeLimit - i)
time.sleep(1)
if timeLimit == 0:
try:
integer_input = int(guess)
except ValueError:
print('Please enter a valid number')
return
global score
if integer_input == a:
print("Correct!")
score += 1
else:
print("Wrong, the answer is",a)
while qLimit != 0:
askQuestion(generateQuestion())
qLimit -= 1
print ("You have", qLimit, "questions remaining")
print("Your score is",score)
if (qLimit == 0):
break
Adapting the example from the Python docs:
import signal
class OutOfTime(Exception):
"too slow"
def handler(signum, frame):
raise OutOfTime("no input received")
try:
# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)
ans = input('What is your favorite color? ')
signal.alarm(0)
except OutOfTime:
print('\nout of time! too slow!')
else:
print('%s, huh? Mine, too!' % ans)
I don't know why but my counter won't stop although I think I have it coded right to stop after three iterations. Any ideas on why it wont work? #Ayush The counter is intialised to 1 at the beginning of the programme which is not included here. Where will I place the conditions? As I've tried using both "for counter in range(3) and while counter in range(3) however in the case of the while counter it runs for 3 iterations and then begins a fourth which gives me an error saying:
Traceback (most recent call last):
File "C:\Python32\Final Year Project\Build 6 (MiniMax input).py", line 311, in <module>
print('The ' + turn + ' has been chosen this time, what move will they make first?!.')
TypeError: Can't convert 'NoneType' object to str implicitly
#CyberGeekk.exe Here's a description of 'turn' and as turn = whoGoesFirst() I've included that aswell.
def whoGoesFirst():
if (counter==1):
if random.randint(1,1) == 0:
return 'computer'
else:
return 'player'
if (counter==2):
if random.randint(1, 1) == 0:
return 'computer'
else:
return 'player'
if (counter==3):
if random.randint(1, 1) == 0:
return 'computer'
else:
return 'player'
theBoard = [' '] * 10
playerLetter, computerLetter = inputPlayerLetter()
turn = whoGoesFirst()
print('Now For Level ' +str(counter)+ '!!')
print('--------------------')
print('The ' + str(turn) + ' has been chosen to go first, what will they do?')
gameIsPlaying = True
while gameIsPlaying:
while counter in range(3):
if turn == 'player':
# Player's turn.
drawBoard(theBoard)
move = getPlayerMove(theBoard)
makeMove(theBoard, playerLetter, move)
if isWinner(theBoard, playerLetter):
counter=counter+1
drawBoard(theBoard)
print('--------------------')
print("No, no! It cannot be! Somehow you tricked me, human. \n"
"But never again! I, the computer,will increse my skill level for round "+str(counter)+" and beat you")
print('--------------------')
print('Now For Level ' +str(counter)+ '!!')
#print('-------------------')
gameIsPlaying = True
theBoard = [' '] * 10
playerLetter, computerLetter = inputPlayerLetter()
turn = whoGoesFirst()
print('The ' + turn + ' has been chosen this time, what move will they make first?!.')
gameIsPlaying = True
The loop breaks on if (counter<=4): because 1 is less then 4. To make the loop go 3 times, simply change the code on that line to look like this: if (counter == 4):. I tried this while commenting out all the unprovided methods and got the loop to go around 3 times.
Hope this helps you in your project, and please leave a comment if you need more help. Your welcome.
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)