How to add for loops in python, in order to not to write the same thing all over? -
name = raw_input("please enter name, know how address you!" " ") semester = int(raw_input("nice meet you" + " " + name + ", " + "please tell me, how many semesters have completed?" " ")) if semester == 1 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) = float(num1)/1 if >= 3.5 : print "good job " + name + ", " + "your gpa great!" if > 3 , <3.5 : print "nice, sure can better," + " " + name + "." if <= 3 , > 2 : print name + " " + "don't give up! should try harder!" if <= 2 : print name + " " + "you better work harder, or on probation!" print name + "'s" + " overall gpa is" + " " + str(a) elif semester == 2 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) = float(num1 + num2)/2 if >= 3.5 : print "good job " + name + ", " + "your gpa great!" if > 3 , <3.5 : print "nice, sure can better," + " " + name + "." if <= 3 , > 2 : print name + " " + "don't give up! should try harder!" if <= 2 : print name + " " + "you better work harder, or on probation!" print name + "'s" + " overall gpa is" + " " + str(a) elif semester == 3 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) = float(num1 + num2 + num3)/3 if >= 3.5 : print ("good job " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) elif semester == 4 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) = float(num1 + num2 + num3 + num4)/4 if >= 3.5 : print ("good job " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) elif semester == 5 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5)/5 if >= 3.5 : print ("good job " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) elif semester == 6 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) num6 = float(input("enter 6-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5 + num6)/6 if >= 3.5 : print ("good job " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) elif semester == 7 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) num6 = float(input("enter 6-th semester gpa: ")) num7 = float(input("enter 7-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5 + num6 + num7)/7 if >= 3.5 : print ("good job, " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) elif semester == 8 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) num6 = float(input("enter 6-th semester gpa: ")) num7 = float(input("enter 7-th semester gpa: ")) num8 = float(input("enter 8-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8)/8 if >= 3.5 : print ("good job, " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) else: print "please enter valid semester number e.g. 1,2,3,...7,8" import sys while true: print('type q exit.') response = input() if response == 'q': sys.exit() print('you typed ' + response + '.')
ok, let's step step:
1. using functions avoid dublicated code
you're using exact same code printing output message, should become function.
def print_response(name, a): if >= 3.5 : print ("good job, " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a))
using function improves code, see below:
name = raw_input("please enter name, know how address you!" " ") semester = int(raw_input("nice meet you" + " " + name + ", " + "please tell me, how many semesters have completed?" " ")) def print_response(a): if >= 3.5 : print ("good job, " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) if semester == 1 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) = float(num1)/1 print_response(a) elif semester == 2 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) = float(num1 + num2)/2 print_response(a) elif semester == 3 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) = float(num1 + num2 + num3)/2 print_response(a) elif semester == 4 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) = float(num1 + num2 + num3 + num4)/4 print_response(a) elif semester == 5 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5)/5 print_response(a) elif semester == 6 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) num6 = float(input("enter 6-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5 + num6)/6 print_response(a) elif semester == 7 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) num6 = float(input("enter 6-th semester gpa: ")) num7 = float(input("enter 7-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5 + num6 + num7)/7 print_response(a) elif semester == 8 : num1 = float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) num2 = float(input("enter 2-nd semester gpa: ")) num3 = float(input("enter 3-rd semester gpa: ")) num4 = float(input("enter 4-th semester gpa: ")) num5 = float(input("enter 5-th semester gpa: ")) num6 = float(input("enter 6-th semester gpa: ")) num7 = float(input("enter 7-th semester gpa: ")) num8 = float(input("enter 8-th semester gpa: ")) = float(num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8)/8 print_response(a) else: print "please enter valid semester number e.g. 1,2,3,...7,8" import sys while true: print('type q exit.') response = input() if response == 'q': sys.exit() print('you typed ' + response + '.')
2. using loop
you can avoid typing code each number of semesters using loop:
name = raw_input("please enter name, know how address you!" " ") semester = int(raw_input("nice meet you" + " " + name + ", " + "please tell me, how many semesters have completed?" " ")) def print_response(a): if >= 3.5 : print ("good job, " + name + ", " + "your gpa great!") if > 3 , <3.5 : print ("nice, sure can better," + " " + name + ".") if <= 3 , > 2 : print (name + " " + "don't give up! should try harder!") if <= 2 : print (name + " " + "you better work harder, or on probation!") print(name + "'s" + " overall gpa is" + " " + str(a)) if semester > 8 or semester < 1: print "please enter valid semester number e.g. 1,2,3,...7,8" else: grade_sum = 0 grade_sum += float(input("ok" + " " + name + ", " + "please enter 1-st semester gpa: ")) in range(2, semester+1): suffix = "-nd" if == 2 else "-rd" if == 3 else "-st" grade_sum += float(input("enter {}{} semester gpa: ".format(i, suffix))) = grade_sum / semester print_response(a) import sys while true: print('type q exit.') response = input() if response == 'q': sys.exit() print('you typed ' + response + '.')
Comments
Post a Comment