[SOLVED] Python - Issues Taking an Input as an Integer (or Converting It)

  • Context: Python 
  • Thread starter Thread starter Ascendant0
  • Start date Start date
  • Tags Tags
    Integer Python
Click For Summary
SUMMARY

The discussion addresses a common issue in Python programming where user input is incorrectly handled as a string instead of an integer. The user attempted to calculate the factorial of an integer input using the math.factorial() function but encountered a TypeError due to improper concatenation in the print statement. The root cause was identified as a failure to convert the input correctly and an oversight in the print statement. The user resolved the issue by correcting the concatenation in the output line.

PREREQUISITES
  • Understanding of Python data types, specifically strings and integers
  • Familiarity with the math module and its functions
  • Basic knowledge of input handling in Python
  • Experience with error messages and debugging in Python
NEXT STEPS
  • Learn about Python string formatting techniques to avoid concatenation errors
  • Explore the try-except block for handling exceptions in Python
  • Study the math.factorial() function and its requirements
  • Investigate Python's input validation techniques to ensure correct data types
USEFUL FOR

This discussion is beneficial for beginner Python programmers, students in programming courses, and anyone encountering input handling issues in their code.

Ascendant0
Messages
176
Reaction score
37
TL;DR
I'm trying to put together code that simply accepts an input of any number, calculates the factorial of the number, and then prints out the resulting value
This is one of the things I've tried:
Python:
import numpy as np
import matplotlib.pyplot as plt
import math

num = int(input("Enter an integer: "))

math.factorial(num)

print("The factorial of this value is " + num + ".")

And this is the resulting error message I get: TypeError: can only concatenate str (not "int") to str

---

I copied that int(input() code right off a site that explained how to do it, set it up identical to how they did, yet it still stores the input as a str instead of an int (and as such, of course can't calculate the factorial). So for some reason, the int() command isn't working for the input?

I also tried changing it to an int after the input was received with these changes:

Python:
num = int(input("Enter an integer: "))

string_value = "num"
integer_value = int(string_value)

math.factorial(integer_value)

print("The factorial of this value is " + integer_value + ".")

Again, I copied the suggestion on how to do it verbatim from a Python site, yet again, another error message: ValueError: invalid literal for int() with base 10: 'num'

There was a couple other things I tried that I can't even find again right now, but what is wrong here? I'm doing exactly what those sites are telling me to do to make the input an int, yet I continue getting some kind of error message or another, every single time???
 
Last edited by a moderator:
Technology news on Phys.org
You guys can completely disregard this, I found my problem. I copied my code from another program, and completely forgot to remove the concatenations in the "print" line. Apologies, I am brand new to all of this as of a few days ago, and I feel the online materials are extremely convoluted and incomplete at times. We're all scrambling in this class, and the professor refuses to help us with any of our code. Wants us to all look it up ourselves. No idea why he even has office hours, but otherwise I really like him, and I love the course (Astrophysics lab)
 
  • Like
Likes   Reactions: jedishrfu and berkeman

Similar threads

  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K