I'm very new to Python and I have several questions.

This is the place for queries that don't fit in any of the other categories.

I'm very new to Python and I have several questions.

Postby Virtual_Anomaly » Sat Aug 23, 2014 6:34 pm

Hi everyone,

My first problem I've been having is how can I print multiple print statements on the same line?
Like if I have...
Code: Select all
print ("1")
print ("2")
print ("3")

This comes out as
Code: Select all
1
2
3

But I want it to come out like...
Code: Select all
1 2 3
Last edited by Yoriz on Sun Aug 24, 2014 1:21 am, edited 1 time in total.
Reason: First post lock, Added code tags.
Virtual_Anomaly
 
Posts: 9
Joined: Sat Aug 23, 2014 6:28 pm

Re: I'm very new to Python and I have several questions.

Postby metulburr » Sat Aug 23, 2014 9:22 pm

If your using python3.x, you can use end in the print functions. Default is a newline.

Code: Select all
print('1', end=' ')
print('2', end=' ')
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: I'm very new to Python and I have several questions.

Postby Baldyr » Sat Aug 23, 2014 9:42 pm

The obvious thing would be not to use several print statements but a single one. You could collect these strings you are printing into a single string and then print the whole thing.

It would also make sense not to use several lines of code for print statements, potentially a large number of them. This is true even if you are printing stuff on several lines, because you should never write repetitive code. Or far worse, write code by copy-paste... :?
User avatar
Baldyr
 
Posts: 284
Joined: Thu Aug 14, 2014 4:15 pm

Re: I'm very new to Python and I have several questions.

Postby Virtual_Anomaly » Mon Aug 25, 2014 2:32 pm

Thanks guys, that worked perfectly!

Ok I'm trying to create a health bar in a game for python but I can't seem to get the proportions right.
Like when the input is current hp 5 and max hp is 10.
I take the symbol for health "+" and multiply it by the current health but how can I make it proportional for however big max hp is?
I mean if I do 100 max health and 25 current health, the health bar should only be a quarter full but it ends up being almost full.

I hope I'm clear enough.
Virtual_Anomaly
 
Posts: 9
Joined: Sat Aug 23, 2014 6:28 pm

Re: I'm very new to Python and I have several questions.

Postby Baldyr » Mon Aug 25, 2014 7:04 pm

Virtual_Anomaly wrote:I hope I'm clear enough.

Your problem sounds pretty straight forward, but you should probably post your code.
User avatar
Baldyr
 
Posts: 284
Joined: Thu Aug 14, 2014 4:15 pm

Re: I'm very new to Python and I have several questions.

Postby Virtual_Anomaly » Mon Aug 25, 2014 7:37 pm

Ok.

plus = "+"
Max = int (input ("Enter your max HP here: "))
Current = int (input ("Enter your current HP here: "))

print ("/----------------------------------------\\")
print ("|", plus * Current, "|")
print ("\\----------------------------------------/")
Virtual_Anomaly
 
Posts: 9
Joined: Sat Aug 23, 2014 6:28 pm

Re: I'm very new to Python and I have several questions.

Postby Baldyr » Mon Aug 25, 2014 8:31 pm

You should use code tags whenever posting code. Not critical for understanding your script in this particular case though.

So your full health bar is 40 characters wide, right. Then you need to know what percentage of the max health your current health is. This is basic math: You divide the current value by the full value. Now, since the fraction will always be less than 1 (one) we're not dealing with integer values anymore. You should probably use floating point values for your input data then. Because then you'll be able to do the math, like:
Code: Select all
full_HP = float(input("full HP"))
current_HP = float(input("current HP"))
fractional_HP = current_HP / full_HP


But in order to print those + characters the desired number of times in the print statement, we need to multiply the fraction with the number of characters making up the full status bar:

Code: Select all
HEALTH_SYMBOL = "+"
BAR_LENGTH = 40
numCharacters = int(fractional_HP * BAR_LENGTH)
print(HEALTH_SYMBOL * numCharacters)


Note that I've converted the floating point values into a integer value before use in the print statement. This will cause the value to be rounded downwards however, which will both mean that only 100% (not 99.99%) health will give you the full 40 characters, while anything below 2,5% will be shown as a empty bar. You probably want to use some other rounding of numbers here, but that is another topic.
User avatar
Baldyr
 
Posts: 284
Joined: Thu Aug 14, 2014 4:15 pm

Re: I'm very new to Python and I have several questions.

Postby Virtual_Anomaly » Mon Aug 25, 2014 9:49 pm

Hey thanks that worked great, I also found out how to make the ASCII art much more organized!
print ("/"+"-"*Hbar+"\\")
print ("|"+plus*Current+" "*int(Hbar-Current)+"|")
print ("\\"+"-"*Hbar+"/")

It's perfect now thanks a lot. I'll post more questions if anything comes up.
Virtual_Anomaly
 
Posts: 9
Joined: Sat Aug 23, 2014 6:28 pm

Re: I'm very new to Python and I have several questions.

Postby metulburr » Mon Aug 25, 2014 10:36 pm

print ("/"+"-"*Hbar+"\\")
print ("|"+plus*Current+" "*int(Hbar-Current)+"|")
print ("\\"+"-"*Hbar+"/")


That is horrible organization of ascii art.

A better approach in python would be the use of python's triple quote strings with format method with the values plugged in by keyword, something along the lines of....
Code: Select all
s = '''
/{HBAR}\\
|{VALUE}
'''.format(
    HBAR  = 'HBAR',
    VALUE = PLUS * CURRENT
)
print(s)


...you get the idea.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: I'm very new to Python and I have several questions.

Postby duanuys » Tue Aug 26, 2014 1:47 pm

result:

1 2 3 4

very simple

In python you can do this:

Code: Select all
print(1, 2, 3, 4)


result will be:
1 2 3 4
Last edited by Yoriz on Tue Aug 26, 2014 5:18 pm, edited 1 time in total.
Reason: First post lock.
duanuys
 
Posts: 1
Joined: Tue Aug 26, 2014 1:44 pm

Re: I'm very new to Python and I have several questions.

Postby Virtual_Anomaly » Tue Sep 09, 2014 6:22 pm

Ok I have ran into a new problem.
I think I understand while loops but I'm still having some issues.
I'm trying to make a checkerboard with draw.rect function. Also trying to make the amount of squares random.

Code: Select all
import pygame
import time
import random

#this is my randoms for how many black squares will appear in my random window, left and right then up and down.
h = (random.randint (5,20))
v = (random.randint (5,15))

pygame.display.init()
wh = (random.randint (400,1000), random.randint (300,800))
screen = pygame.display.set_mode((wh))
pygame.draw.rect (screen, (255,255,255), ((0,0),wh))

i=0
while i<500:
   #not sure how to or what to put in for the x and y values to make the rectangles spaced apart properly.
    pygame.draw.rect(screen, (0,0,0), (?,?,60,30))
    i+=1

pygame.display.flip()
time.sleep(5)
pygame.display.quit()



Also how do you put up code in there own code box on this website?
Last edited by metulburr on Tue Sep 09, 2014 8:48 pm, edited 1 time in total.
Reason: added code tags
Virtual_Anomaly
 
Posts: 9
Joined: Sat Aug 23, 2014 6:28 pm

Re: I'm very new to Python and I have several questions.

Postby metulburr » Tue Sep 09, 2014 8:49 pm

m trying to make a checkerboard with draw.rect function. Also trying to make the amount of squares random.

What does this code have anything to do with this post. This is completely different than the original post. Thus requires a whole new thread. Its confusing if you just keep posting new questions on the same thread.

how do you put up code in there own code box on this website?

If you read my New Users link in my signature, it explains how to post code in code tags.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests