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

  • Like 2

Share this post


Link to post
Share on other sites
32 minutes ago, kichilron said:

Save the conversion to string and change the comparisons to:

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

Edited by DannyDog
  • Like 1

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

Alright guys, it's working! Thanks everyone!

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
  • Like 2

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
  • Like 1

Share this post


Link to post
Share on other sites
3 hours ago, DannyDog said:

@.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.

Indeed, only started a week ago. 

  • Like 1

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

×