Question

Using Python 3, is there a way I could find the second_highest value of a user's...

Using Python 3, is there a way I could find the second_highest value of a user's random input using a while-loop? For example, if I use random input numbers such as: 100,95,92,87,83,80,75,50,40 etc. I want it to compute the 2nd highest along with my highest and lowest score. Is there any if, else, elif, etc statement that I could add to my while statement or in my while-loop to make this happen? The output would be something like highest = 100, lowest 40, and second_highest 95 if using the above random inputs. I'd like to avoid using any list operations [ ] and def functions. Thank you.

score = 0

highest = 0                  #highest score

second_highest = 0     #second highest score

lowest = 0                    # lowest score

count = 0

total = 0

score = float(input("Enter your first score (0 to quit): "))   ##using random input example of 100,95,92,87,83,80,75,50,40
while score != 0 :
   value = float(score)
   if (lowest == 0):
      lowest = value
      highest = value
      second_highest = value    ##how can I add a statement or two that can help find 2nd highest score?
   if value < lowest:
      lowest = value
      print ("The lowest is now: ",lowest)


   if value > highest:

      second_highest = value
      highest = value     
      print ("The highest has been updated to: ", highest)  
   total = total + score
   count = count + 1

   score = float(input("Enter another score: "))

print("The highest score: ",highest)                        ## should be 100

print("Lowest score: ",lowest)            ## should be 40

print("Second highest score", second_highest)    ## output should be 95

## thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

score = 0
highest = 0 #highest score
second_highest = 0 #second highest score
lowest = 0 # lowest score
count = 0
pre_value=0.0
total = 0
score = int(input("Enter your first score (0 to quit): ")) ##using random input example of 100,95,92,87,83,80,75,50,40
while score != 0 :
   value = float(score)
   if (lowest == 0):
       lowest = value
       highest = value
       second_highest = pre_value ##second value is 0
   if value < lowest:
       lowest = value

   if value > highest:
       highest = value   
  
   if pre_value!=highest and second_highest<pre_value: #if previous value is not highest and greater than second highest value
       second_highest=pre_value

   pre_value=score #previous value
   score = int(input("Enter another score: "))

if (pre_value!=highest and pre_value>second_highest): #if last number(previous value) is greater than second highest
   second_highest=pre_value
  
print("The highest score: ",highest) ## should be 100
print("Lowest score: ",lowest) ## should be 40
print("Second highest score", second_highest) ## output should be 95

Add a comment
Know the answer?
Add Answer to:
Using Python 3, is there a way I could find the second_highest value of a user's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a python program to play rock paper scissors using a while loop and if statements....

    Create a python program to play rock paper scissors using a while loop and if statements. I have started but have gotten stuck in a continuous loop. Please look over my code and help me find where I went wrong. Here it is: import random #Choice weapons=['Rock' ,'Paper' ,'Scissors'] print('Rock, Paper, Scissors!') print('Rock, Paper, Scissors!') print('Shoot!') human=input('Choose Rock, Paper, Scissors, or Quit! ') print('')#Blank Line while human != 'Quit': human_choice=human computer=random.choice(weapons) print(computer) if human==computer: print("It's a tie!") elif human=='Rock': if...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot...

    Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot is a computer program designed to emulate human conversation. For this program you will use if statements, user input, and random numbers to create a basic chatbot. Here is the scenario: You have decided to start an online website. You are creating a prototype to show investors so you can raise money and launch your website. You should ask the user at least 5...

  • I am having trouble with my Python code in this dictionary and pickle program. Here is...

    I am having trouble with my Python code in this dictionary and pickle program. Here is my code but it is not running as i am receiving synthax error on the second line. class Student: def__init__(self,id,name,midterm,final): self.id=id self.name=name self.midterm=midterm self.final=final def calculate_grade(self): self.avg=(self.midterm+self.final)/2 if self.avg>=60 and self.avg<=80: self.g='A' elif self.avg>80 and self.avg<=100: self.g='A+' elif self.avg<60 and self.avg>=40: self.g='B' else: self.g='C' def getdata(self): return self.id,self.name.self.midterm,self.final,self.g CIT101 = {} CIT101["123"] = Student("123", "smith, john", 78, 86) CIT101["124"] = Student("124", "tom, alter", 50,...

  • Questin 1 (CO 2) Which of the following is not a valid name in python? last_name...

    Questin 1 (CO 2) Which of the following is not a valid name in python? last_name lastName last name lname Question 2 (CO 3) How many times will “Hello World” be displayed after the following code executes? num=2 while num<12:     print("Hello world")     num+=2 2 12 5 4 Question 3 (CO 1) The following code contains an error, what type of error is it and what line number is it? 1 count=1 2 while count<4 3    print("count = ",...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • this code is not working for me.. if enter 100 it is not showing the grades...

    this code is not working for me.. if enter 100 it is not showing the grades insted asking for score again.. #Python program that prompts user to enter the valuesof grddes . Then calculate the total scores , #then find average of total score. Then find the letter grade of the average score. Display the #results on python console. #grades.py def main(): #declare a list to store grade values grades=[] repeat=True #Set variables to zero total=0 counter=0 average=0 gradeLetter='' #Repeat...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT