Jump to content
DMentMan

Python Coding - Help Me Please

Recommended Posts

Hello, I only recently got into coding in Python and I've got a problem I can't solve so I hope someone might be able to help me here. =D

I want my simple piece of code to say that if you have over 8000 it responds with that you can pay the rent. If you have under 8000 I want it to respond with that you can't pay the rent.

I've pretty much got it working after a few issues being fixed, but if the input is 30 it says I can't pay the rent for some reason... Anyways here is the code, and thanks for any feedback or help =D

(And yes I know like 5 and 7 are not needed, but they fixed some problems when I put 10000 as the input it said I can't afford to pay the rent for some reason =P)

 

def Money():
  Money = input("How much money do you have?")
  str(int(Money))
  if Money > "8000":
    print("You can pay the rent!")
  elif Money > "10000":
    print("You can pay the rent!")
  elif Money == "10000":
    print("You can pay the rent!")
  elif Money == "8000":
    print("You can pay the rent!")
   elif Money < "8000":
    print("You can't afford to pay the rent!")
  else:
    print("Error")
Money()

def exit1():
#Exits program after question has been answered

 exit1()

exit()

Share this post


Link to post
Share on other sites

One question:

why do you convert from int to string on the money-variable?

Strings are texts, such as this reply to your post.

Integers are numbers, and only with numbers the comparison works reliably.

 

I don't know how this works in Python, but some languages / compilers represent strings / texts in a way that 30 may end up being greater than 10,000 - just because they're converted into UNICODE or ASCII.

 

Save the conversion to string and change the comparisons to:
Money > 8000


The reason you do this is a similar one as described above - "" <-- these represent strings, if you try to compare an integer with "8000" it won't work, as you're comparing a number (int) with text (String).
This may even be the reason you're converting to string.

 

If you change the things mentioned above - then it might work

Share this post


Link to post
Share on other sites
1 hour ago, kichilron said:

One question:

why do you convert from int to string on the money-variable?

A friend told me to do it and I just left it like that =D   I'll try to redo it with the info you provided in mind =)

30 minutes ago, DannyDog said:

To be more specific @dmentman he meant just convert Money to an integer using int(Money) and NOT str(int(Money))

 Replacing str(int(Money)) with int(Money) gives an error =/

 

 

Thanks guys, I'll let ya know if it works =D

Share this post


Link to post
Share on other sites

Sorry but i have to be a bit of a dick here

 

 

def money(): # Functions/Methods ONLY lowercase (camelcase or underscore if more then one word)
  input_text = input("How much money do you have?") # same for variables; and dont use the function name as variable name
  amount = int(input_text) # you have to assign this to a variable to work
  if amount > 8000: # one empty line after an complete if block
    print("You can pay the rent!")
    
  elif amount > 10000:
    print("You can pay the rent!")
    
  elif amount == 10000:
    print("You can pay the rent!")
    
  elif amount == 8000:
    print("You can pay the rent!")
    
  elif amount < 8000:
    print("You can't afford to pay the rent!")
    
  else:
    print("Error")
    
money()

def exit1(): # doesnt make sense in this context? and please use sys.exit(exitcode)
 exit1()

exit()

 

PS: Use code brackets

Edited by .freaK

Share this post


Link to post
Share on other sites
54 minutes ago, dmentman said:

Alright guys, it's working! Thanks everyone!

That's great! Also for extra points you can shorten your code by using >= and <= operators which checks if the input is ALSO EQUAL TO the compared value.

Example would be:

  if Money >= 8000:
    print("You can pay the rent!")

Which means if Money is bigger than OR EQUAL TO 8000. Print the message.

So you can get rid of the == checks.

But personally if i were to code it I would just do

if Money < 8000:

 print("You cannot afford to pay the rent!")

else:

 print("You can pay the rent!")

@.freaK Nah you're not being a dick. OP probably just hasn't learnt proper coding conventions and its good that you bring it up.

Edited by DannyDog

Share this post


Link to post
Share on other sites

int(Money)

why the two lines about "1000" ??

all you need is:


if       ...      >   


elif     ...     ==    


elif     ...     <

 

- unless you're going to add in that with 200 or more spare you can have a wild night out after you pay the rent?

xxp

 

 

Edited by pilgrim*
if ... >= ... # Danny D correct,natch

Share this post


Link to post
Share on other sites

One big thing I noticed is why are there so many elif's? Only if Money >= 8000, elif Money < 8000 and else would work. If someone would enter 8000 or above it's going to say "You can pay the rent!". If they enter anything under 8000 it will say "You can't afford to pay the rent!". The things @.freaK mentioned are useful feedback as well.

I advise you to look up naming conventions and such for Python or general programming and use that as early as possible. It will not only help you but also potential other coders who have to review or work with your code.

Good luck learning Python! Programming can be very fun but at the same time very frustrating. I think Python is a good language to start with.

On 9/15/2017 at 11:04 AM, kichilron said:

One question:

why do you convert from int to string on the money-variable?

Strings are texts, such as this reply to your post.

Integers are numbers, and only with numbers the comparison works reliably.

 

I don't know how this works in Python, but some languages / compilers represent strings / texts in a way that 30 may end up being greater than 10,000 - just because they're converted into UNICODE or ASCII.

 

Save the conversion to string and change the comparisons to:
Money > 8000


The reason you do this is a similar one as described above - "" <-- these represent strings, if you try to compare an integer with "8000" it won't work, as you're comparing a number (int) with text (String).
This may even be the reason you're converting to string.

 

If you change the things mentioned above - then it might work

The Python interpreter doesn't fall over the fact you try to compare an int to a string since it's an interpreted language. With any compiled languages the compiler will throw an exception when trying to compile the code.

Share this post


Link to post
Share on other sites


 "I was looking for a hobby programming project to keep me occupied during the week around Christmas. I chose 'Python' as a working title for the project, being a big fan of Monty Python's Flying Circus."  - Guido van Rossum, 1989. <this probably explains a lot>.

pythonic:

    Beautiful is better than ugly
    Explicit is better than implicit
    Simple is better than complex
    Complex is better than complicated
    Readability counts

https://en.wikipedia.org/wiki/Zen_of_Python

 

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Similar Content

    • By S3V3N
      Hi, I'm a 3D Modeler and I want to supply a knowleadgable coder/scripter with content for a future mod of Dayz.
       
      I can create everything 2D and 3D-wise necessary for the mod, but right now I'm overwhelmed by the editors available for ARMAII and I'll be having a hard time learning everything by myself. I just want to supply content at this point and learn from the people I'm working with, in order to make my content perfect for their needs. On the long run, I'll probably be able to do more than supply the models and textures, but I need someone to set me up, in order to continue working in a way that makes sense. 
       
      I want to create an US-American map, preferably Oregon or Arkansas styled, but I'm willing to put that aside and work on your project for the sake of learning. If anyone out there needs models to be made for their mod and knows the coding part/integration of assets/maps, I'm more than willing to deliver. When the time comes for Modding support in DayZ, we can be ready and perhaps get a bit more into depth with our own project (adding new functionality and such).
       
      I'd also be happy about just meeting and chatting with other people interested in modding DayZSA, but the goal is to find someone who can instruct me on the how-to, so I'm not wasting time iterating at this early stage of my Dayz modding career ;)
       
      If you're interested and confident about making mods for the standalone, send me a PM and we'll see what can be done at the moment. I need info on integrating assets as well as on lightmapping, collisions, sockets, dynamic objects. I'll start reading into those topics for ARMAII, but I know there are people advanced in these topics, who could help me far more by giving a few hints.
       
      Here are a couple of (random) images of stuff I made, so you know I'm not wasting your time:
       
       
       
      Thanks and looking forward to your replies!
       
      I posted in the wrong place again :(? Thank you, Boneboys, for being so good at keeping track of my nonsense :)
    • By GeneralWampa
      Is DayZ written in C++? If not, what is it written in?
       
      Thanks,
      GeneralWampa
    • By heroinaddict
      I feel like the amount of money that you guys have grossed from the opening of standalone should make you want to increase the development team. The DayZ dev team is very undermanned for the amount of people that you need to please, hiring more developers would help immensely and we all know you have the money to do it. It is taking far too long for simple updates to go through and the more developers, the easier. I mean, you sold $5.1 million in a day, that should call for a higher staff team for the demand that the DayZ community has set out. I know multiple people who don't even play anymore because of the lack thereof of things to do. All there is, is PVP which gets annoying. Cars aren't even in sights yet as we only have a handful guns in the game ATM. And only a few of them are decent. Hiring some amazing coders to help you deal with server issues would help immensely and help the community stop complaining about simple fixes. I know this game is brutally hard to code, don't get me wrong. But the community base for this game is ridiculous and you guys need more staff to code this game.
×