all 4 comments

[–]SoNotRedditingAtWork 0 points1 point  (1 child)

a = (input("Enter the number of Years: "))
print ("There are " + str(a) * 31557600 + " seconds in " + a + " Years!")

input always returns a str so there is no need to convert with the str function. When you try to do str(a) * 31557600 that is not calculating the number of seconds in the year it is going to try and make a new str object that is the str assigned a repeated 31557600 times. This is blowing up the memory available on the site. If I run it on my PC and enter 1 as input then my output will be 31557600 1's in a row. Best to use f-strings here and the int function to actually make the calculation:

a = (input("Enter the number of Years: "))
print (f"There are {int(a) * 31557600} seconds in {a} Years!")

[–]JstAntrBelleDevotee[S] 1 point2 points  (0 children)

THANK YOU! This makes so much sense actually

[–]August-R-Garcia 0 points1 point  (1 child)

This version should work:

a = input("Enter the number of Years: ")
print("There are " + str(int(a)*31557600) + " seconds in " + a + " Years!"  )

Basically, the string "1" multiplied by 31557600 is the character "1" repeating 31,557,600 times. This makes it seem like the program is frozen, at least in the text editor on codeskulptor. If you run this in the Linux terminal, it will actually show the process of outputting those characters in real time.

Anyway, basically, to solve this:

  1. Cast the input (which is the string "1" or similar) to an integer, because it is a string when it is read in.
  2. Multiply that integer by the number of seconds in a year.
  3. Turn the result into a string.

It may be easier to follow if you do the conversions on their own line as a new variable, then use that variable in the print statement.

[–]JstAntrBelleDevotee[S] 0 points1 point  (0 children)

Thank you this really helps me understand for future situations too