In: Computer Science
Can anyone fix this code? The code isn't rounding the answer to the nearest whole number like 2.345 should be 2 and 2.546 should be 3 but the round() function isn't working as expected. The round() function is rounding 2.546 to 2 which is incorrect since 4 and below should be rounded to 2 and 5 and above should be rounded to 3.
Here is the code:
#importing xlwt library to write into xls
import xlwt
from xlwt import Workbook
#create a workbook
wb = Workbook()
#create a sheet
sheet = wb.add_sheet('Sheet')
#percentages list
percentages = [23.6, 38.2, 50, 61.8, 78.6, 113, 123.6, 138.2,
161.8]
#add first row
for i in range(len(percentages)):
sheet.write(0,i+1,str(percentages[i])+'%')
#user input
n = int(input('Enter number of elements: '))
#second row starts from index 1
row=1
print('Enter numbers: ')
for i in range(n):
#User input
val = float(input())
#Add entered value to first column of the row
sheet.write(row,0,str(val))
#calculate each percentage
for j in range(len(percentages)):
result =
(percentages[j]/100)*val
#write result to sheet by rounding
upto 3 decimal points
sheet.write(row,j+1,str(round(result,3)))
#increment the row by 1
row+=1
#save Workbook as xls`
wb.save('percentages.xls')
Thank you so much for anyone who helped!
I have showed some sample on how round function behaves.. It has a second argument which signfies the level of rounding on how many digits you want after decimal.. In your case, You want 2.5 or more to be rounded as 3, while 2.4999999999 and below to be 2.. Which means, You do not want a digit after decimal.. Hence you should call round function with second argument as 0.. which will give you desired result.. Below is complete code:
#importing xlwt library to write into xls import xlwt from xlwt import Workbook #create a workbook wb = Workbook() #create a sheet sheet = wb.add_sheet('Sheet') #percentages list percentages = [23.6, 38.2, 50, 61.8, 78.6, 113, 123.6, 138.2, 161.8] #add first row for i in range(len(percentages)): sheet.write(0,i+1,str(percentages[i])+'%') #user input n = int(input('Enter number of elements: ')) #second row starts from index 1 row=1 print('Enter numbers: ') for i in range(n): #User input val = float(input()) #Add entered value to first column of the row sheet.write(row,0,str(val)) #calculate each percentage for j in range(len(percentages)): result = (percentages[j]/100)*val #write result to sheet by rounding upto 3 decimal points sheet.write(row,j+1,str(round(result,0))) #increment the row by 1 row+=1 #save Workbook as xls` wb.save('percentages.xls')
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.