+ 2

How do I convert the str to int without an error

def grading(): if student_score <40: print("Grade: F") elif student_score <46: print("Grade: E") elif student_score <50: print("Grade: D") elif student_score <60: print("Grade: C") elif student_score <70: print("Grade: B") elif student_score >70: print("Grade: A") student_score=int(input("Enter the student score or enter to exist")) total_score=0 number_of_students=0 grading() while student_score != "": total_score+= student_score number_of_students+=1 student_score=int(input("Enter the student_score or enter to exist")) grading () print("number of students =",number_of_students) average= total_score / number_of_students print("Average=",average)

6th Jul 2020, 1:21 PM
Ajisegiri Sunday
Ajisegiri Sunday - avatar
11 Antworten
+ 3
7th Jul 2020, 6:41 PM
Russ
Russ - avatar
+ 3
You're closing the while loop when the input is "" (nothing), but you try and cast the input to int before checking that there is anything in the string. score = input() while score != "": score = int(score) # do_stuff score = input()
6th Jul 2020, 1:45 PM
Russ
Russ - avatar
+ 2
I think you might have done this. Still, you can see the following code:- s=int(input()) print(s)
6th Jul 2020, 1:23 PM
Arctic Fox
Arctic Fox - avatar
+ 1
You get invalid literal error, when the input isn't an integer. In your while loop, you are comparing student_score with a string and it isn't a string...
6th Jul 2020, 1:34 PM
Arnesh
Arnesh - avatar
+ 1
AKSHAY I know. I am currently using pydroid3
7th Jul 2020, 6:01 PM
Ajisegiri Sunday
Ajisegiri Sunday - avatar
+ 1
Guys help I still can't correct the error anyone to help
7th Jul 2020, 6:02 PM
Ajisegiri Sunday
Ajisegiri Sunday - avatar
+ 1
Russ thanks very much it runs just fine..I will compare it to mine and make the necessary adjustments. Thanks
7th Jul 2020, 6:49 PM
Ajisegiri Sunday
Ajisegiri Sunday - avatar
0
Yeah I did that it keeps telling me ValueError: invalid literal for int() with base 10: ''
6th Jul 2020, 1:28 PM
Ajisegiri Sunday
Ajisegiri Sunday - avatar
0
Adeniran not sure but I think this may be due sololearn input problem as your program needs more than 1 input and it requires all input to be given in starting at time of running, if you are not giving proper input then it will cause error..
7th Jul 2020, 3:56 PM
AKSHAY🇮🇳
AKSHAY🇮🇳 - avatar
- 1
Hi
8th Jul 2020, 1:50 AM
Christian désire Kouassi
Christian désire Kouassi - avatar
- 2
Use type casting property Example:- Store="58" restore=int(store) print(restore)
6th Jul 2020, 4:39 PM
shubham kumar
shubham kumar - avatar