New Posts  All Forums:Forum Nav:

Python Homework Help

post #1 of 7
Thread Starter 
Number Analysis Program
Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in a list and then display the following data:
- The lowest number in the list
- The highest number in the list
- The Total of the numbers in the list
- The average of the numbers in the list

You must use a loop that loops 20 times and appends each score to a list after it is entered.
Code:
#
#This program will ask the user to enter a series of 20 numbers. It will then
#store the numbers in a list and then display the lowest number in the list,
#the highest, the total, and the average number in the list while using a loop.
#

def main():
    numbers = get_values()
    get_values()
    get_analysis(numbers)
    
def get_values():
    print('Please Enter A Series Of 20 Random Numbers')
    values =[]    
    for i in range(20):
        value =(int(input("Enter A Random Number " + str(i + 1) + ": ")))
        values.append(values)
               
    return values

def get_analysis (numbers):
    print("The Lowest Number Is: ") + str(min(numbers))
    print("The Highest Number Is: ") + str(max(numbers))
    print("The Sum The Numbers Is: ") + str(sum(numbers))
    print("The Average The Numbers Is:  ") + str(sum(numbers)/len(numbers))

main()

Big Blue
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066 
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H 
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518 
Mouse Pad
X-TRAC Pad Ripper 
  hide details  
Reply
Big Blue
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066 
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H 
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518 
Mouse Pad
X-TRAC Pad Ripper 
  hide details  
Reply
post #2 of 7
Is there something specific that you need help with?
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #3 of 7
Code:
def main():
    numbers = get_values()
    # get_values()   # not needed, you already called it 
    get_analysis(numbers)
    
def get_values():
    print('Please Enter A Series Of 20 Random Numbers')
    values =[]    
    for i in range(20):
        value =(int(input("Enter A Random Number " + str(i + 1) + ": ")))
        values.append(value)  # append value not values,  simple typo, but it makes a difference
               
    return values

def get_analysis (numbers):

    # your parentheses are in the wrong place http://docs.python.org/3.1/library/functions.html#print

    print("The Lowest Number Is:",  min(numbers))
    # print("The Lowest Number Is: ") + str(min(numbers))

    print("The Highest Number Is:", max(numbers))
    # print("The Highest Number Is: ") + str(max(numbers))

    print("The Sum The Numbers Is:", sum(numbers))
    # print("The Sum The Numbers Is:") + str(sum(numbers))

    print("The Average The Numbers Is:", sum(numbers)/len(numbers))
    # print("The Average The Numbers Is:  ") + str(sum(numbers)/len(numbers))

main()

assuming this is python 3
Edited by tom.slick - 11/30/13 at 10:00pm
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #4 of 7
Thread Starter 
Oh sorry I forgot to post my error I was getting.
Code:
Traceback (most recent call last):
  File "F:/Python33/Beckett, Allan Assignment #9.py", line 30, in <module>
    main()
  File "F:/Python33/Beckett, Allan Assignment #9.py", line 13, in main
    get_analysis(numbers)
  File "F:/Python33/Beckett, Allan Assignment #9.py", line 25, in get_analysis
    print("The Lowest Number Is: ") + str(min(numbers))
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Big Blue
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066 
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H 
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518 
Mouse Pad
X-TRAC Pad Ripper 
  hide details  
Reply
Big Blue
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066 
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H 
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518 
Mouse Pad
X-TRAC Pad Ripper 
  hide details  
Reply
post #5 of 7
Thread Starter 
Quote:
Originally Posted by tom.slick View Post

Code:
def main():
    numbers = get_values()
    # get_values()   # not needed, you already called it 
    get_analysis(numbers)
    
def get_values():
    print('Please Enter A Series Of 20 Random Numbers')
    values =[]    
    for i in range(20):
        value =(int(input("Enter A Random Number " + str(i + 1) + ": ")))
        values.append(value)  # append value not values,  simple typo, but it makes a difference
               
    return values

def get_analysis (numbers):

    # your parentheses are in the wrong place http://docs.python.org/3.1/library/functions.html#print

    print("The Lowest Number Is:",  min(numbers))
    # print("The Lowest Number Is: ") + str(min(numbers))

    print("The Highest Number Is:", max(numbers))
    # print("The Highest Number Is: ") + str(max(numbers))

    print("The Sum The Numbers Is:", sum(numbers))
    # print("The Sum The Numbers Is:") + str(sum(numbers))

    print("The Average The Numbers Is:", sum(numbers)/len(numbers))
    # print("The Average The Numbers Is:  ") + str(sum(numbers)/len(numbers))

main()

assuming this is python 3

Thanks for the help, my book was suggesting that str was to be used. Your code seemed to work and had no problems.
Thanks again,
ALLAN
Big Blue
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066 
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H 
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518 
Mouse Pad
X-TRAC Pad Ripper 
  hide details  
Reply
Big Blue
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q6600 EVGA NVIDIA nForce 780i SLI EVGA GeForce GTX 470 OCZ Platinum 4x2GB 1066 
Hard DriveOptical DriveOSMonitor
WD 640GB & WD300GB ATAPI DVD/CD Reader Windows 7 Ultimate x64 Asus VH242H 
KeyboardPowerCaseMouse
Logitech G15 ABS Tagan BZ Series BZ700 700W Antec 1200 Logitech MX518 
Mouse Pad
X-TRAC Pad Ripper 
  hide details  
Reply
post #6 of 7
In general, str.format is better to use when formatting strings. get_values can be rewritten using str.format:
Code:
def get_values():
    print('Please Enter A Series Of 20 Random Numbers')
    values =[]    
    for i in range(20):
        value = int(input("Enter A Random Number {}: ".format(i+1)))
        values.append(value) 
               
    return values
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #7 of 7
Quote:
Originally Posted by xXxALLANxXx View Post

Thanks for the help, my book was suggesting that str was to be used. Your code seemed to work and had no problems.
Thanks again,
ALLAN

Yes you can use str to turn a int into a string, but again it comes back to the way you were doing it
notice the placement of the parentheses (extra space added to make the parentheses stand out)

wrong way
Code:
print ( "The Lowest Number Is: " ) + str ( min ( numbers ) )
in the above you have 2 seperate functions on the same line, print() and str(), both seperate.

correct way
the placement of parentheses makes a differents
Code:
print ( "The Lowest Number Is: " + str ( min ( numbers ) ) )
or
Code:
print ( ( "The Lowest Number Is: " ) + str ( min ( numbers ) ) )
the print function will print what is between the parentheses,
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming