If statement to check if the value of a variable is in a JSON file [closed]

2024/11/17 11:55:03

I'm kind of new so try not to judge me. I'm trying to create a little 2d game based on the old 2d Mario. I already have home window, the sign up and login windows, and I've got a json file to save the usernames and passwords. Now, I'm trying to get the login function to work. The problem seems to be this line:

if plpaword in players['password']

This should help you understand what the variables stand for

f = open('player.json')
players = json.load(f)
plpaword = E2.get()

When I run my code, everything else seems to work fine until I try to log in. After logging in, it should create a new window, but what happens is nothing happens and it gives the error:

Exception in Tkinter callback
Traceback (most recent call last):File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__return self.func(*args)File "/Users/kevin/PycharmProjects/ANewFile/ANewFile.py", line 101, in LIpasswords = players['password']
KeyError: 'password'

I used these modules:

from tkinter import *
import tkinter
import tkinter.messagebox
from tkinter import messagebox
from tkinter import Tk, Button, Frame, Entry, END
import random
import json

Creating an account and saving it to my player.json file:

    def SU():try:plusname = E1.get()plpaword = E2.get()plpaword2 = E3.get()plemail = E4.get()if plpaword == plpaword2:if plusname in players:messagebox.showerror(random.choice(error), "An account with that username already exists. ""Please choose another.")else:players[plusname] = {'password': plpaword, 'email': plemail}with open('player.json', 'w') as f:json.dump(players, f)signup.destroy()messagebox.showinfo("Account Created!", "Please log in to your new account through log in.")else:messagebox.showerror(random.choice(error), "Passwords did not match. Please try again")except:messagebox.showerror(random.choice(error), random.choice(errormsg))
Answer

According to the code of SU(), the content inside the JSON file (also the content of players) should be something like below:

{"user1": {"password": "pass1", "email": "[email protected]"},"user2": {"password": "pass2", "email": "[email protected]"}
}

So the checking should be:

plusname = E1.get()
plpaword = E2.get()
for player in players:if plusname == player and plpaword == players[plusname]["password"]:print("login successful")break
else:print("login failed")

That is players["password"] should be players[plusname]["password"] instead.

https://en.xdnf.cn/q/120205.html

Related Q&A

Assign variables to a pandas dataframe when specific cell is empty

I am assigning some variables to values from a data frame. The data frame created using this code data = [[tom, 10], ["", 15], [juli, 14]] df = pd.DataFrame(data, columns=[Name, Age])So after…

Try Except for one variable in multiple variables

I am reading every row in a dataframe and assigning its values in each column to the variables The dataframe created using this code data = [[tom, 10], [, 15], [juli, 14]] df = pd.DataFrame(data, colum…

Print method invalid syntax reversing a string [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 q…

How QRegularExpression can be passed to Qt::MatchRegularExpression

I am trying this sample code that I found which is really really good. I am also trying to figure out the same thing to find an item and scroll to it, but this time I wanted to match the the string whi…

Python list rearrange the elements in a required order

My main list has 44 names as elements. I want to rearrange them in a specific order. I am giving here an example. Note that elements in my actual list are some technical names. No way related to what I…

GMB API accounts.locations.reviews.list

Can i get source code to get reviews using gmb python code also heard mybusiness is discontinued. I tried using my business api is discontinued can i get the implementation process to look for extract…

How to get words from an online text archive such as pastebin?

Im trying to get the words(users) from a text file hosted online, such as from www.site.com/mytextfile.txt or pastebin.com/raw/1111111. I will have multiple "users", one in each line. My code…

Python Histogram using matplotlib on top words

I am reading a file and calculating the frequency of the top 100 words. I am able to find that and create the following list:[(test, 510), (Hey, 362), ("please", 753), (take, 446), (herbert, …

how can I add field in serializer?

Below is my serializer.py file: from rest_framework import serializersclass TaskListSerializer(serializers.Serializer):id = serializers.CharField()user_id = serializers.CharField()status = serializers.…

Read and write a variable in module A from module B

Need a solution or workaround to read and write the variable in moduleA from moduleB please. Here is the code:moduleAimport moduleBvariable = 10def changeVariable():global variablevariable = 20def main…