How can I print variable and string on same line in Python?

后端 未结 18 2148
面向向阳花
面向向阳花 2020-11-28 01:45

I am using python to work out how many children would be born in 5 years if a child was born every 7 seconds. The problem is on my last line. How do I get a variable to work

18条回答
  •  悲哀的现实
    2020-11-28 02:07

    I copied and pasted your script into a .py file. I ran it as-is with Python 2.7.10 and received the same syntax error. I also tried the script in Python 3.5 and received the following output:

    File "print_strings_on_same_line.py", line 16
    print fiveYears
                  ^
    SyntaxError: Missing parentheses in call to 'print'
    

    Then, I modified the last line where it prints the number of births as follows:

    currentPop = 312032486
    oneYear = 365
    hours = 24
    minutes = 60
    seconds = 60
    
    # seconds in a single day
    secondsInDay = hours * minutes * seconds
    
    # seconds in a year
    secondsInYear = secondsInDay * oneYear
    
    fiveYears = secondsInYear * 5
    
    #Seconds in 5 years
    print fiveYears
    
    # fiveYears in seconds, divided by 7 seconds
    births = fiveYears // 7
    
    print "If there was a birth every 7 seconds, there would be: " + str(births) + " births"
    

    The output was (Python 2.7.10):

    157680000
    If there was a birth every 7 seconds, there would be: 22525714 births
    

    I hope this helps.

提交回复
热议问题