Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Assignment # 3 – try – except and casting Part 1: Widgets sell for $17 ea

ID: 3860435 • Letter: P

Question

Python Assignment # 3 – try – except and casting

Part 1:

Widgets sell for $17 each

Using raw_input, ask the user for the number of widgets required

Use try – except to verify that the input value is an int

If the value entered is valid,

Calculate the cost of the widgets

If the value entered is NOT valid, close the program       # See the example above

Part 2:

Sales tax for widgets is currently 5.75%

Calculate the cost of the widgets, including tax

Print the total cost in sentence form.

Note: You must use raw_input to receive full credit

Explanation / Answer

#--code start----------------------------
print 'List values are : ', List[0], List[1],List[2],List[3],List[4]
#--------------------------------------
f=open('temp.txt','w')
f.write(List[0])
f.close()
#--code ends-----------------------------

Note:- I dont know which kind of array is this
So please help me to solve this problem

Regards
Nakul

Reply

Peter Bengtsson

18 May 2006

Replace
f.write(List[0])
with
f.write(str(List[0]))

Reply

Moe

19 March 2008

Hi hows it going?
im trying to open a note pad file using python, the commands i use , load the program but when i choose which ever questionare it doesnt load it can u please let me know the problem? because i got a mid term in 5 days thnx alot the code is the following:
def IQ():
x=0
while x!=10:
score =0
myfile= open('IQ.txt', 'r')
question = myfile.readline()
answer1 = myfile.readline()
answer2 = myfile.readline()
answer3 = myfile.readline()
answer4 = myfile.readline()
correct = myfile.readline()
explanation = myfile.readline()
points=float(myfile.readline())
print question
print "1-"+ answer1
print "2-"+ answer2
print "3-"+ answer3
print "4-"+ answer4
answer=raw_input("what's your answer?")
answer=answer.rstrip()
x=x+1
if answer== correct[0]:
print" Right",
score+=points
else:
print" Wrong",
print explanation
print "score:",score," "
def Random():
x=0
while x!=10:
score =0
myfile= open('Random.txt', 'r')
question = myfile.readline()
answer1 = myfile.readline()
answer2 = myfile.readline()
answer3 = myfile.readline()
answer4 = myfile.readline()
correct = myfile.readline()
explanation = myfile.readline()
points=float(myfile.readline())
print question
print "1-"+ answer1
print "2-"+ answer2
print "3-"+ answer3
print "4-"+ answer4
answer=raw_input("what's your answer?")
answer=answer.rstrip()
x=x+1
if answer== correct[0]:
print" Right",
score+=points
else:
print" Wrong",
print explanation
print "score:",score," "

total=0
while total==0:
print "1- IQ"
print "2- Random"
choice=(raw_input("which game you wana play:"))
try:
choice=int(choice)
if choice==1:
IQ()
total=total+1
if choice==2:
Random()
total=total+1
else:
print"you should enter either 1 or 2"
total=0
except:
print "you should enter a number"
total=0