Python3 Calculate summation of while loop output - python-3.x

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

Related

How do I output the name alongside the second smallest number only using while loops

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])

python program which return max count of chacharter like input=aabbbcczaaaabbbccc ouput=a3b3c3z1

python program which return max count of chacharter like input=aabbbcczaaaabbbccc ouput=a3b3c3z1
st=aabbbcczaaaabbbccc
ouput=a3b3c3z1
def maxCount(st):
return
maxCount(st)
If you meant max consecutive times, then this code will work (it returns a dictionary):
def maxCount(st):
counts = dict()
last = ''
tmp = 0
for c in st:
if last == c:
tmp += 1
elif last != '':
if last in list(counts.keys()):
counts[last] = max(counts[last], tmp)
tmp = 1
else:
counts[last] = tmp
tmp = 1
last = c
return counts
print(maxCount('aabbbcczaaaabbbccc'))

how to count how many element changed its position in a list after sorting?

I want to print the count of numbers that have changed their position.
My code:
def mysort(arr):
count = 0
for i in range(len(arr)):
min_value = i
for j in range(i, len(arr)):
if arr[j] < arr[min_value]:
min_value = j
count += 1
temp = arr[i]
arr[i] = arr[min_value]
arr[min_value] = temp
return count
my_list = [4,2,3,1,6,5]
print(mysort(my_list))
My code is returning 3 as output but it should return 4. How can I fix it?
To count the number of elements with positions changed, why not try:
def count_change(old, new):
# assume old and new have the same length
count = 0
for x,y in zip(old, new):
if x != y:
count += 1
return count
Your code actually counts the number of value swap

Program: finding the percentage / hackerrank

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))

Adding Numbers in a DOB?

How do I make it add the numbers separately to the user's input? for example: 120409 = 25 I am new to python. Why isn't the total working?? Right now nothing is printed, is there something wrong with my indentation?
me = int(input("enter your dob 230478"))
def dob(me):
dob = []
count = 0
me = str(me)
for i in range(len(me)):
dob.append(me[i])
for i in range(len(dob)):
dob[i] = int(dob[i])
count += dob[i]
total = count + me
print(dob,me)
Try this:
usrInp = str(input("enter your dob 230478: "))
def method1(usrInp):
count = 0
for digit in usrInp:
count = count + int(digit)
print(count)
method1(usrInp)

Resources