ValueError: invalid literal for int() with base 10: python [closed]

2024/11/18 20:33:07

ValueError: invalid literal for int() with base 10: ''

Why it showing the error of int(), in fact the values coming from the cgi is as string which i am converting it to integer, bcoz my comparable part variable actual_ans_dict contains an integer

res12 = form.getvalue('opt_12', '')
res27 = form.getvalue('opt_27', '')
res20 = form.getvalue('opt_20', '')
res16 = form.getvalue('opt_16', '')
res13 = form.getvalue('opt_13', '')
res19 = form.getvalue('opt_19', '')
res25 = form.getvalue('opt_25', '')actual_ans_dict = {}
count = 0
b = []
for data in prsnobj.result:actual_ans_dict[data[0]] = data[1]#print actual_ans_dict[12], actual_ans_dict[27], actual_ans_dict[20],     actual_ans_dict[16], actual_ans_dict[13], actual_ans_dict[19], actual_ans_dict[25]if int(res12) == actual_ans_dict[12]:count += 1
if int(res27) == actual_ans_dict[27]:count += 1
if int(res20) == actual_ans_dict[20]:count += 1
if int(res16) == actual_ans_dict[16]:count += 1
if int(res13) == actual_ans_dict[13]:count += 1
if int(res19) == actual_ans_dict[19]:count += 1
if int(res25) == actual_ans_dict[25]:count += 1
if count:b.append(count)if len(b)==0:print "Fail"else:print "Marks: ", b
Answer

The problem is because int tries to convert '' to a base 10 number, which is not possible. Thats why it is failing. You are getting the default '' if the value is not there like this

form.getvalue('opt_12', '')

instead of that use a sentinel value like this

form.getvalue('opt_12', '0')

Even better, you can convert them to numbers as and when you get them from the form like this

res12 = int(form.getvalue('opt_12', '0'))
...
...
...
https://en.xdnf.cn/q/120030.html

Related Q&A

python code to connect to sftp server

I found this code to connect to remote sftp server with the help of username ,password and host but i also need to include the port number, can any one let em know how to include the port number in thi…

Python: get the max value with the location above and below than the max

If I have a dataframe like this, index User Value location1 1 1.0 4.5 2 1 1.5 5.23 1 3.0 7.04 1 2.5 7.55 2 1.0 11.56 2 1.…

Retrieve smart cards PAN with Python and pyscard

Im trying to retrieve the PAN of a smart card using pyscard in Python. What I have done so far is to connect to the reader and to retrieve various information about the reader and the card... but I can…

How to stop a specific key from working in Python

My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didnt work, I tried programming a code that can stop it but i couldnt because i am a beginner…

How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

trying to write a Python function: def compare_lengths(x, y, z) which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length. For example, if the funct…

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…

.LAS into a .CSV file using python

How to change a .las file into a .csv file? Have been trying myself but no luck no far. I am just looking for something decently short that will save some time when I have to convert big .olas files i…

using pandas read_excel to read from stdin

Note: I have solve this problem as per below:I can use to_csv to write to stdout in python / pandas. Something like this works fine:final_df.to_csv(sys.stdout, index=False)I would like to read in an a…

How to print a string x times based on user input [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Understanding for loops in Python(4 answers)Closed 1 year ago.I am trying to make a simple application that will pri…

How does .join work in Python?

I want to display each row of a SQL query result on a webpage. I found some code, but I dont understand what this line does.u"<br>".join([u"{0}".format(row.combination) for r…