How to use while on Tkinter to make Par or Odd game?

Asked

Viewed 330 times

1

I’m starting now and so I’m super lost, this and my code:

from random import choice
from tkinter import *

janela = Tk()
janela.title("Jogo do PAR ou IMPAR!")
fontPadrao = ("Arial", "10", "bold")
def jogar():
    pc = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    escolha = ''
    while True:
        cont = 0
        pc_escolha = choice(pc)
        pc_escolha1["text"] = pc_escolha
         jogador = int(jo.get())
         jo.delete(0, END)
         print(jogador)
         print(pc_escolha)
        while escolha != 'P' and escolha != 'I':
            escolha = str(parimpar2.get()).upper().strip()
            parimpar2.delete(0, END)
            digitar["text"] = "Informe [P]PAR ou [I]IMPAR!"
        digitar["text"] = ""
        resu = (jogador + pc_escolha) % 2
        if escolha == 'P' and resu == 0:
            vencedor["text"] = "Jogador Venceu!"
            cont += 1
        elif escolha == 'P' and resu == 1:
            vencedor["text"] = "Computador Venceu! Voce venceu {} 
vezes".format(cont)
            break
        if escolha == 'I' and resu == 1:
            vencedor["text"] = "Jogador Venceu!"
            cont += 1
        elif escolha == 'I' and resu == 0:
            vencedor["text"] = "Computador Venceu! Voce venceu {} 
vezes".format(cont)
            break            
jo = Entry(janela, width=5)
jo["bg"] = "#D3D3D3"
jo.place(x=165, y=40)

bt = Button(janela, text="Jogar", width=20, command=jogar)
            bt["font"] = ("Arial", "10", "bold")
            bt.place(x=100, y=250)

            Jogador = Label(janela, text="Jogador")
            Jogador.place(x=10, y=40)

            parimpar = Label(janela, text="Escolha PAR ou IMPAR[P/I]")
            parimpar.place(x=10, y=80)

            parimpar2 = Entry(janela, width=5)
            parimpar2["bg"] = "#D3D3D3"
            parimpar2.place(x=165,y=80)

            Computador = Label(janela, text="Computador ")
            Computador.place(x=10, y=110)

            pc_escolha1 = Label(janela, text="")
            pc_escolha1.place(x=165, y=110)

            vencedor = Label(janela, text="",  font=fontPadrao)
            vencedor.place(x=125, y=190)

            digitar = Label(janela, text="")
            digitar.place(x=10, y=140)

            janela.geometry("600x300+500+200")
            janela.mainloop()

And this error is appearing: inserir a descrição da imagem aqui

Someone can help me????

  • I suppose jo.get() is returning something that is not an integer, so the int raises this exception. It would be similar to int("ABC")

  • what I do not and why there is not returned an integer being that gives this error even before I put a new value

  • Try to change that line: jogador = int(jo.get()) by is, to better understand what is coming from the get method of the jo object. resposta = jo.get()
print(resposta)
if resposta: #faça algo
print(resposta)
else:
print(resposta)
 #não há resposta nada que resposta esteja referenciando.

  • so I did what you said and it’s returning the number I typed

  • If you are talking about the number your image shows, https://i.stack.Imgur.com/dZMEG.png Almost sure that jo.get() is returning an empty string and you trying to convert with int(') end up generating such an error. Check if the return of jo.get is a real string number or if it is an empty string. Here’s how I duplicated the error, only when trying to convert an empty string: >>> int('')&#xA;Traceback (most recent call last):&#xA; File "<stdin>", line 1, in <module>&#xA;ValueError: invalid literal for int() with base 10: ''

  • @Glove you are filling the field "player" on the screen?

  • yes, I want when making the loop the number I typed disappear and I can put a new value and I want that loop to continue until the I lose and the win pc showing how many consecutive wins I had

  • Until you resolve the fact that jo.get() is not returning a string with a number inside, you will continue to follow this error. The right thing in my opinion would be to stop converting jo.get() to integer, leaving that way: ... . pc_escolha1["text"] = pc_escolha&#xA; jogador = jo.get()&#xA; jo.delete(0, END).. Segundo https://www.tutorialspoint.com/python3/tk_entry.htm the get method returns a text input -- which in your case is being empty. So int() raises an error saying it cannot convert empty string to number.

  • i ever thought of taking the int so the problem is that by the time the code arrives at that part resu = (player + pc_choice) %2 to check if and odd or odd will give error

  • Then click to see if the input is valid, that is, make sure that the string contains only numbers to work with the conversion. Try using resources from the string class. if jo.get(). isdigit(): #do something, make sure it only contains numbers in that entry. If you enter this if then convert to player numbers = int(jo.get())

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.