[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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 3K views
Ascendant0
Messages
179
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:
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)
 
Reply
  • Like
Likes   Reactions: jedishrfu and berkeman