Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • How to add a date when a record is created SQL
  • YouTube channel lists – Python DataFrames Python Dataframe
  • Python Dictionary Interview Questions Python
  • ValueError: pattern contains no capture groups Value Error
  • What Is An Array In Python? array
  • Welcome to Data Analytics Ireland Livestream
  • How to Group By in a Select Statement SQL
  • how to update records in SQL CRUD

TypeError: not all arguments converted during string formatting

Posted on February 18, 2023March 12, 2023 By admin

Estimated reading time: 2 minutes

In our latest post, we are going to discuss formatting issues that you may come across. It relates to how you format the data when passing it from the input of a user.

This scenario can happen as a result of user input, which then has some comparisons completed on it.

How does the problem occur?

The problem occurs when you pass the data, but do not format it correctly before you present it in the output.

In the below code you will see these lines:

print ("'{0}' is older than '{1}'"% age1, age2)
print("'{0}'is younger than '{1}', that looks correct!" % age1, age2)

The specific problem is that when you put the % before age1, and age 2 you will get this TypeError.

Why does the problem occur?

This problem occurs as the way you are referencing age1 and age2 has been deprecated.

For reference please look at PEP 3101 – Advanced String Formatting, but essentially if you are using % it is the old way of achieving this!

How can the problem be fixed?

So to fix this please look at the below code:

age1 = input("Please enter your age: ")
age2 = input("Please enter your father's age: ")

if age1 == age2:
    print("The ages are the same, are you sure?!")
if age1 > age2:
    #print ("'{0}' is older than '{1}'"% age1, age2)
    print("'{0}' is older than '{1}', you better check!" .format(age1, age2))
if age1 < age2:
    print("'{0}'is younger than '{1}', that looks correct!" % age1, age2)
    #print("'{0}'is younger than '{1}', that looks correct!".format(age1, age2))

As you can see, I have included the old incorrect code and the new correct code, and when I use the proper Python logic, it gives me the correct output as follows:

Please enter your age: 25
Please enter your father's age: 28
'25'is younger than '28', that looks correct!

Process finished with exit code 0
Python, type error Tags:Data, Python, python arguments, TypeError

Post navigation

Previous Post: How to use zip() function in Python
Next Post: TypeError: expected str, bytes or os.PathLike object, not DataFrame

Related Posts

  • How to Generate Random Integers Between 0 and 9 Python
  • How to sort a Python Dictionary Python
  • Python Tutorial: Pandas groupby columns ( video 2) Python
  • TypeError: Array() Argument 1 Must Be A Unicode Character, Not List array
  • What are Lambda functions in Python? Python
  • How To Add Values to a Python Dictionary Python

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • How to pass multiple lists to a function and compare Python Functions
  • Import a CSV file with an SQL query CSV
  • select rows with a certain value using SQL SQL
  • TypeError: expected str, bytes or os.PathLike object, not DataFrame Python
  • How to Pass Python Variables to Javascript Javascript
  • How To Fix TypeError: unhashable type ‘slice’ python dictionaries
  • how to create an instance of a class class
  • What are measures in Tableau? data visualisation

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT