• Welcome to Autism Forums, a friendly forum to discuss Aspergers Syndrome, Autism, High Functioning Autism and related conditions.

    Your voice is missing! You will need to register to get access to the following site features:
    • Reply to discussions and create your own threads.
    • Our modern chat room. No add-ons or extensions required, just login and start chatting!
    • Private Member only forums for more serious discussions that you may wish to not have guests or search engines access to.
    • Your very own blog. Write about anything you like on your own individual blog.

    We hope to see you as a part of our community soon! Please also check us out @ https://www.twitter.com/aspiescentral

Another coding question

The mighty Boosh

Well-Known Member
Hey guys

I'm stuck with a few things in Python. Trying to write a shop for a simple game but can't seem to setup my arguments after user has picked an option.



money=5
redshirt=5
greenshirt=7

print (" (1) Red shirt £5 ")

print (" (2) Green shirt £7 ")

user = raw_input ("What whould you like to buy : ")

if user == "1":

This is the part that get me - how do I get it to check money vs cost and either tell the player they can't afford it or add it to thair gear ?
Have done this in batch but something's diffrent in python and shell script.


in batch I'd do this after the user has picked

:REDSHIRT

cls

if %moneys% LSS 5 ( goto SORRY )

set /a redshirt+=1
set /a money-=5

goto SHOP


Thanks and sorry in advance - I'm still new to this and not 100% sure what or how to ask .
 
You're probably better off asking in a Python-related Reddit page or something like that. You'd probably get fast answers there.
 
You could add a variable called cost.

Then, under the condition that the user has chosen a shirt(s), have the cost calculated.
Then use a comparison operator with money being how much the user has and cost being the (final) cost of the item(s).

If money < cost then tell the user that they don't have enough money to buy the shirt(s).
 
Hello. There are some things I would like to note before adding the code

(1) raw_input is no longer used in Python3. Now it's just input.
(2) input (and the formerly raw_input) saves the input in the variable as a string. This strings needs to be converted to a number, an integer in this case, in order to perform numeric operation (such as < or >).
(3) If you already set the variables beforehand, it's redundant to make the algorithm check how much money the user has before buying something.

But I think this is something close to what you asked: Version 1

I also wrote a small script based on your idea here: Version 2

Please ask me if you have any questions.
 
Last edited:
# website shifted the lines


# sorry , know I was asking lots and probebly setting off someones OCD - sorry for the input i will get around to python 3

# Kinda sorted the math part out ?

money=10
item1=5
storage=0

def shop () :

counter=input("Enter command : ")

IF counter =="1" :

print (" not enoght money ")
shop( )
ELIF money > item1 :
print ("you have brought item 1 ")

global money
global item1
global storage

money -= item1
storage+=item1

shop( )

shop( )

# Just wanted to ask if this was a good way and could you pass around some examples please ?
# That's awesome - How did you send that ?

# Also what moduals whould you recomend looking at ?
 
How did you do that copy and paste link ? That's spot on

why did i use global ? seems to have worked

feels like hogwarts spells class - You're a wizard Harry
 
How did you do that copy and paste link ? That's spot on

why did i use global ? seems to have worked

feels like hogwarts spells class - You're a wizard Harry
Just paste the code in Paste ofCode and click "Paste it". Then copy the link here.

I tried to read your post before but I couldn't. Since python syntax is very much dependent on indentation, without additional elements to specify structure, it's hard to read any code without it. I can't try either without the indentation.

Global variables will work in and outside functions, but it is usually considered a bad programming practice. It doesn't make much of a difference when writing short programs, but when something doesn't work it will make it very much harder to find out where the code is wrong.
 
Sorry about that it was early in the morning (had a few drinks) when i went through the responces.

Thankyou guys I've been stuck trying to get python to handle math for ages, kept bugging out or running without changing anything.

money = money - redshirt

I did try this but kept getting something along the lines of refrenced before assignment or it would run but wouldn't change any values - until I found someone useing the global part.

print("You now have £"+str(money))

Oh also you've awnserd another question . How to print text and variable , tho it won't let me use £.

Pick your brain once more - Can you run shell script or python on an android tablet without it being rooted ?

@Misery sorry I got exited . Should have used stack overflow but felt safer asking on here (bad at putting thought to paper)
 

New Threads

Top Bottom