Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the integers entered but it says it is in string format?

num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
num3 = input("Enter third number: ")
num4 = input("Enter fourth number: ")
num5 = input("Enter fifth number: ")
print("\n")
print("%d,%d,%d,%d,%d" %("num1,num2,num3,num4,num5"))

Results:

Enter first number: 90
Enter second number: 98
Enter third number: 94
Enter fourth number: 98
Enter fifth number: 99
Traceback (most recent call last):
  File "<string>", line 7, in <module>
TypeError: %d format: a number is required, not str

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want to use them as integers later on, you’ll need to convert the input to int type like this:

Also, natice that the quotes around the variable in the string formatting have been removed. The quotes were causing the python interpreter to think it was just being passed a single string.

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
num4 = int(input("Enter fourth number: "))
num5 = int(input("Enter fifth number: "))
print("\n")
print("%d,%d,%d,%d,%d" %(num1,num2,num3,num4,num5))

If you don’t need to use the input as integers later on you can just leave them as strings and change the string formatting to use them as strings:

num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
num3 = input("Enter third number: ")
num4 = input("Enter fourth number: ")
num5 = input("Enter fifth number: ")
print("\n")
print("%s,%s,%s,%s,%s" %(num1,num2,num3,num4,num5))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading