Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3552

Python • Re: Program that works in windows not working on pi

$
0
0
None of that code by itself does anything.

Post the entire code.
main.py:

Code:

import jsonstartmoney = 1000data = {}def read_file():    f = open("data.txt", "r")    file_contents = f.read()    f.close()    return(file_contents)def write_file(content):    f = open("data.txt", "w")    f.write(json.dumps(content))    f.close()def add_save(key, value):    data = json.loads(read_file())    data[key] = value    write_file(data)    print(f"Added user: {key} with balance: {value}")  # Print confirmation message    return "done"def get_value(username):    data = json.loads(read_file())    print(f"Loaded data: {data}")  # Print the loaded data dictionary    if data.get(username) == None:        add_save(username, startmoney)    else:        print(f"Found user: {username}")  # Print if user exists    return data.get(username)def pay_user(from_user, to_user, amount):    data = json.loads(read_file())    if data.get(from_user) is None:        data[from_user] = startmoney        write_file(data)    if data.get(to_user) is None:        data[to_user] = startmoney        write_file(data)    if not int(data.get(from_user)) >= int(amount) or int(amount) <= 0:            return("problem with request")    else:        data[from_user] = int(data.get(from_user)) - int(amount)        data[to_user] = int(data.get(to_user)) + int(amount)        write_file(data)        return("successful")        
and cloudrequests.py

Code:

import scratchattach as scratch3import mainsession = scratch3.login("wvzack", "############")conn = session.connect_cloud("#####") #replace with your project idclient = scratch3.CloudRequests(conn)@client.requestdef ping(): #called when client receives request    return "pong" #sends back 'pong' to the Scratch project@client.requestdef get_balance():    try:        balance = main.get_value(client.get_requester())        formatted_message = f"You have {balance} dollar(s)"        return balance    except Exception as e:        print(f"Error retrieving balance for {client.get_requester()}: {e}")        return "Error retrieving balance. Check the Python console for details."    @client.requestdef pay(to, amount):    print(client.get_requester())    print(to)    print(int(amount))    return_value = main.pay_user(str.lower(client.get_requester()), str(to), amount)    return return_value  # Simply return the value from main.pay_user()    @client.eventdef on_ready():    print("Request handler is running")client.run() #make sure this is ALWAYS at the bottom of your Python file

Statistics: Posted by wvzack — Tue Jun 04, 2024 2:06 am



Viewing all articles
Browse latest Browse all 3552

Trending Articles