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

  • Thread starter Thread starter Ascendant0
  • Start date Start date
  • Tags Tags
    Integer Python
AI Thread Summary
The discussion centers around a user's struggle with Python code for calculating the factorial of an integer input. The user initially encounters a TypeError due to attempting to concatenate a string with an integer in the print statement. Despite following online examples, the user is confused about why the input is stored as a string instead of an integer. They attempt to convert the input to an integer after receiving it, but face a ValueError because they mistakenly treat the variable name as a string. Eventually, the user realizes that the issue stemmed from not properly formatting the print statement, leading to the errors. They express frustration with the learning process and the lack of support from their professor, despite enjoying the course.
Ascendant0
Messages
175
Reaction score
38
TL;DR Summary
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 jedishrfu and berkeman
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top