Python Forum
First program issues with format display
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First program issues with format display
#1
Good evening,

I am very basic at programming in python and am seeking help in finishing my first program.
If possible help me with.
1.The welcome message. I want it to display as the image below
2. The end result so it is displayed continuously on one line like the image below.

Thank you.

****************************************
Things I've tried
implicit continuation
print(average_player) + (display_avg)
print ()
#Line 34
Explicit continuation
print(average_player) \
+ (display_avg)
#Line 34
print(average_player) + "'s average hit" + display_avg)
print ()

#I've changed the average_player variable to only display the name or display the name and the string "'s average hit"

**************************
<a href="https://ibb.co/cwA7Jb"><img src="https://thumb.ibb.co/cwA7Jb/Baseball_Team_management.jpg" alt="Baseball_Team_management" border="0"></a>
<img>
https://ibb.co/cwA7Jb
</img>
The program should display as above.

Below is my source code.
-------------------------------------------------------------------------------------
#!/usr/bin/env python3

import locale

#display a welcome message
print("===============================================================")
print ()
print(" Baseball Team Manager")
print ()
print("This program calculates the batting average for a player based")
print ()
print("on the player's official number of at bats and hits.")
print ()
print("================================================================")
print ()

choice = "y"
while choice.lower() == "y":

# get input from the user
player_names = str(input("Player's name:\t"))
official_at_bat = int(input("Official number at bat:\t"))
number_of_hits = int(input("Number of hits:\t"))
average_player = str(input(player_names)+ "'s batting average is")

# convert the players batting average
average_hit = float(
number_of_hits / official_at_bat)

#Average hits
display_avg = round(average_hit,3)

# display the batting avrage
print(average_player)
print(display_avg)
print ()
print ()
# see if the user wants to continue
print
choice = input("continue? ( y/n): ")
print ()

print("Bye!")
----------------------------------------------------------------------------------
Reply
#2
#!/usr/bin/env python3

import locale

#display a welcome message
print("===============================================================")
print ()
print("                            Baseball Team Manager")
print ()
print("This program calculates the batting average for a player based")
print ()
print("on the player's official number of at bats and hits.")
print ()
print("================================================================")
print ()

choice = "y"
while choice.lower() == "y":
  
    # get input from the user
    player_names = str(input("Player's name:\t"))
    official_at_bat = int(input("Official number at bat:\t"))
    number_of_hits = int(input("Number of hits:\t"))
    average_player = str(input(player_names)+ "'s batting average is")

    # convert the players batting average
    average_hit = float(
        number_of_hits / official_at_bat)

    #Average hits
    display_avg = round(average_hit,3)
        
    # display the batting avrage
    print(average_player) 
    print(display_avg)
    print ()
    print ()
    # see if the user wants to continue
    print
    choice = input("continue? ( y/n): ")
    print ()

print("Bye!")
Reply
#3
Try this
#!/usr/bin/env python3
# import locale
import os


def header():
    # display a welcome message
    os.system('cls')
    print("===============================================================")
    print()
    print("                            Baseball Team Manager")
    print()
    print("This program calculates the batting average for a player based")
    print()
    print("on the player's official number of at bats and hits.")
    print()
    print("================================================================")
    print()

def body():
    choice = "y"
    while choice.lower() == "y":
        header()
        # get input from the user
        player_names = str(input("\nPlayer's name:\t"))
        official_at_bat = int(input("Official number at bat:\t"))
        number_of_hits = int(input("Number of hits:\t"))
        average_player = str(input("{}'s batting average is ".format(player_names)))

        # convert the players batting average
        average_hit = float(
            number_of_hits / official_at_bat)

        # Average hits
        display_avg = round(average_hit, 3)

        # display the batting avrage
        print('--------------- stats ---------------')
        print('Battiing Average: {}'.format(average_player))
        print('Averate hitsL {}\n\n'.format(display_avg))
        # see if the user wants to continue
        choice = input("continue? ( y/n): ")
    print("Bye!")

body()
Reply
#4
Thank you for your replay. However, would rather the final statement match the image https://ibb.co/cwA7Jb I will try to figure it out with your code Thank you!!!! At least with your program {player_name}'s bat average is displays on the same line

Larz60

(variable).format display was the solution I needed thank you. however can I get it to display two variables on the same line so the result is
{players}'s is batting average is {rounded average}
Reply
#5
#!/usr/bin/env python3
#import locale

import locale

def header ():
    # display a welcome message
    print("===============================================================")
    print("                            Baseball Team Manager")
    print ()
    print("This program calculates the batting average for a player based")
    print("on the player's official number of at bats and hits.")
    print ()
    print("================================================================")

def body():
    choice = "y"
    while choice.lower() == "y":

        header()
        #get input from the user
        print()
        player_names = str(input("Player's name:\t"))
        official_at_bat = int(input("Official number at bat:\t"))
        number_of_hits = int(input("Number of hits:\t"))

        #convert the players batting average
        average_hit = float(
            number_of_hits / official_at_bat)

        #average hits
        display_avg = round(average_hit,3)
        
        #display bat average
        print()
        print(format(player_names) + "'s batting average is " + format(display_avg))
        print()

        #see if the user wants to continue
        choice = input("continue? ( y/n): ")

    print("Bye!")
This is the final result. Thank you for your help!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020