I have been trying to fix my code and have not been able to do so. My assignment asks me to output the second smallest score along with the student's name. I got the score part right but not the name part. Can you please help me?
n = int(input("Enter number of student: "))
minscore = 1000
i = 1
name = 0
while i <= n:
print ("Please enter the name of student: ",i)
x = str(input())
print ("Please enter the score of student: ",i)
y = int(input())
if y < minscore:
minscore2 = minscore
minscore = y
name = x
elif y < minscore2:
minscore2 = y
i += 1
print ("Second lowest score is",x,"with score",minscore2)
Your problem is you need to save the lowest name with the score.
n = int(input("Enter number of student: "))
minscore = 1000
i = 1
name = ''
name2 = 0
while i <= n:
print ("Please enter the name of student: ",i)
x = str(input())
print ("Please enter the score of student: ",i)
y = int(input())
if y < minscore:
minscore2 = minscore
minscore = y
name2 = name
name = x
elif y < minscore2:
minscore2 = y
name2 = x
i += 1
print ("Second lowest score is",name2,"with score",minscore2)
A better way to go about this is to use lists and the .sort() method
n = int(input("Enter number of students: "))
names_with_scores = []
i = 1
while i <= n:
names_with_scores.append([input("Please enter the name of student number " + str(i) + " : "), input("Please enter the score of student number " + str(i) + " : ")])
# ^^^ Add name and scores to list inside a list ^^^ #
i += 1
def sort_func(e):
return e[1]
names_with_scores.sort(key=sort_func)
# Sorts the list in order of score #
print ("Second lowest score is " + str(names_with_scores[1][0]) + " with a score of " + names_with_scores[1][1])
x = int(input("Enter the number: "))
count = 0
for elements in range(0,10):
for i in (x):
if elements == i:
count += 1
break
print()
That error raise because an integer is not an iterate object unlike strings, list, etc. So what you can do it's just work with a string, then use set (which get the unique values) and then get the length of it and you won't need to traverse with a for loop as below:
x = input("Enter the number: ")
unique_digits = set(x)
print(len(unique_digits))
Hope it will help you :)
x = input("Enter number: ")
count = 0
for elements in range(10):
for t in x:
if (int(t)==elements):
count += 1
break
print(count)
I am using below python code:
n = int(input('enter the number:'))
student_marks = {}
for i in range(n):
name, *line = input().split()
scores = list(map(float, line))
student_marks[name] = scores
query_name = input('enter the name:')
list_1 = list(student_marks[query_name])
no = len(l)
print(no)
s = sum(l)
print(s)
ss = s/no
print(ss)
But, i am getting an error while input the query_name during the run of code.
source: https://www.hackerrank.com/challenges/finding-the-percentage/problem
you can try to do
n = int(input('enter the number:'))
student_marks = {}
for i in range(n):
name, *line = input("enter name and scroe (spared by space): ").split()
scores = list(map(float, line))
student_marks[name] = scores
query_name = input('enter the name:')
list_1 = list(student_marks[query_name])
no = len(list_1)
print("the numer of scores {}".format(no))
s = sum(list_1)
print("The sum of all scores {}".format(s))
ss = s/no
print("The average score {}".format(ss))
if __name__ == '__main__':
n = int(input())
student_marks = {}
count = 0
for _ in range(n):
name, *line = input().split()
scores = list(map(float, line))
student_marks[name] = scores
query_name = input()
for i in student_marks[query_name]:
count += i
average = count / len(student_marks[query_name])
print("%.2f" %average)
You can try this solution:
--------------------------
from decimal import Decimal
# Main function (like Java main() method)
if __name__ == '__main__':
# Taking number of times input will be taken from console and converting it into int type
n = int(input())
# creating an empty dictionary
student_marks = {}
# Iterate from: 0 to n-1 times
for _ in range(n):
# Taking the first argument as name and all other numbers inside line var
name, *line = input().split()
# Converting the numbers contained in line variable to a map then, converting into list
scores = list(map(float, line))
# Inserting into dictionary as key - value pair
student_marks[name] = scores
# Taking the student name from console and store into query_name
query_name = input()
# Fetch student marks using student name
query_scores = student_marks[query_name]
# Sum all the marks
total_scores = sum(query_scores)
# Find average of the marks
avg = Decimal(total_scores/3)
# print the average upto two decimal point
print(round(avg, 2))
I've to create a program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. The program should display an appropriate error message if the first value entered by the user is 0.
Note: Number of inputs by the user can vary. Also, 0 marks the end of the
input it should not be included in the average
x = int(input("Enter Values\n"))
num = 1
count = 0
sum = 0.0
if x == 0:
print("Program exits")
exit()
while (x>0):
sum += num
count += 1
avg = (sum/(count-1))
print("Average: {}".format(avg))
You were not taking input inside while loop. You were taking input on the first line for once. So your program was not taking input repeatedly.
You may be looking for this -
sum = 0.0
count = 0
while(1):
x=int(input("Enter Values: "))
if x == 0:
print("End of input.")
break;
sum+=x;
count+=1;
if count == 0:
print("No input given")
else:
avg = sum/count;
print("Average is - ",avg)
Your code does not work because int function expect only one number.
If you insert the numbers one by one, the following code works:
num = int(input("Enter a value: "))
count = 0
sum = 0.0
if num <= 0:
print("Program exits")
exit()
while (num>=0):
sum += num
count += 1
num = int(input("Enter a value: "))
avg = (sum/count)
print(f"Average: {avg}")
I just started with python3 and tried this idea of assigning number to alphabets and calculate the total.
Eg: if input is "Hi" my output should come "6" (H is 5 and I is 1 so total is 6)
I do not know how to sum the output of while loop output.
name = input("Enter Your name ")
name =name.upper()
name = list(name)
print(name)
items = {'A':'1', 'I':'1', 'J':'1', 'Q':'1','Y':'1',
'B':'2', 'K':'2', 'R':'2',
'C':'3', 'G':'3', 'L':'3', 'S':'3',
'D':'4', 'M':'4', 'T':'4',
'E':'5', 'H':'5', 'N':'5', 'X':'5',
'U':'6', 'V':'6', 'W':'6', 'O':'7', 'Z':'7', 'F':'8', 'P':'8', '.':'0'}
counter = 0
x = len(name)-1
while counter <=x:
names = name[counter]
if names in items:
new_name = (items[names])
else:
print('no')
name_int = int(new_name)
print(name_int)
counter = counter +1
This should work for you:
name = input("Enter Your name ")
name =name.upper()
name = list(name)
print(name)
items = {'A':'1', 'I':'1', 'J':'1', 'Q':'1','Y':'1',
'B':'2', 'K':'2', 'R':'2',
'C':'3', 'G':'3', 'L':'3', 'S':'3',
'D':'4', 'M':'4', 'T':'4',
'E':'5', 'H':'5', 'N':'5', 'X':'5',
'U':'6', 'V':'6', 'W':'6', 'O':'7', 'Z':'7', 'F':'8', 'P':'8', '.':'0'}
counter = 0
x = len(name)-1
total = 0
while counter <=x:
names = name[counter]
if names in items:
new_name = (items[names])
total += int(new_name)
else:
print('no')
counter += 1
print(total)
However, you can write your code in a more pythonic way:
name = input("Enter Your name ")
items = {'A':1, 'I':1, 'J':1, 'Q':1,'Y':1,
'B':2, 'K':2, 'R':2,
'C':3, 'G':3, 'L':3, 'S':3,
'D':4, 'M':4, 'T':4,
'E':5, 'H':5, 'N':5, 'X':5,
'U':6, 'V':6, 'W':6, 'O':7, 'Z':7, 'F':8, 'P':8, '.':0}
total = 0
for ch in name.upper():
total += items(ch) if ch in items else 0
print total