1

The objective is to write a program that will increase the population every 7 and 35 seconds and decrease every 13 seconds. I am trying to use a loop for this program and I am having some problems with getting the right casting for each variable. Here's the code:

#(1)There is a birth every 7 seconds (2)There is a death every 13 seconds (3)There is a new    
immigrant every 35 seconds.
#CURRENT POP: 307,357,870

populationCurrent = input("What is the current population")
x=0
while x!=100:
    if (x%7==0):
        populationCurrent=populationCurrent+1
        x=x+1
    elif (x%13==0):
        populationCurrent=populationCurrent-1
        x=x+1
    elif (x%35==0):
        populationCurrent+=1
        x=x+1
    else:
        x=x+1
print("The population will be "+int(populationCurrent)+".")

Thank you for your time.

Alex Haugen
  • 9
  • 1
  • 2
  • Python doesn't have type casting. – chepner Sep 04 '14 at 17:06
  • Python does not have casting per se. – Ignacio Vazquez-Abrams Sep 04 '14 at 17:06
  • `x = x + 1` is executed for *every branch*. You can just move that out of the `if` statements and not repeat yourself. – Martijn Pieters Sep 04 '14 at 17:07
  • I don't understand what your question is. Is your code not running, or not giving you the expected result? It looks like you never decrement the population. – Lukeclh Sep 04 '14 at 17:08
  • 1
    Your code also handles the various events incorrectly. They are *not exclusive*; 35 is divisible by 7 too, but your code either has a birth *or* an immigration. At 91 seconds, there should be both a birth and a death, but your code will only register a birth at that moment. – Martijn Pieters Sep 04 '14 at 17:14

5 Answers5

1

I think you are confused in python2 and python3, there's a difference in input() function of python 2.x and python 3.x, where input() function gives an integer value in python 2 and str in python 3

Vikas Mishra
  • 65
  • 1
  • 7
1
  1. input() is str by default so, this should be converted to int

    populationCurrent = str(input("What is the current population"))

  2. You cannot concatenate string and int

    print("The population will be "+str(populationCurrent)+".")

  3. Its easier to do this than iterate through 100 times

    populationCurrent += 100//7 + 100//35 - 100//13

Ashoka Lella
  • 6,631
  • 1
  • 30
  • 39
  • @JoranBeasley, I tested it without the int-cast, it returns float, converting `populationCurrent` itself to float – Ashoka Lella Sep 04 '14 at 17:30
  • if you do `//` it should floor it to an int ... (see http://ideone.com/BclA4S) (although its not a big deal either way results in the correct solution :P) – Joran Beasley Sep 04 '14 at 17:30
0

You need to convert populationCurrent to an integer immediately after you read the string.

populationCurrent = int(input("What is the current population"))

Note that if you don't enter a string that is a valid integer representation, this will raise a ValueError. You might want to consider how to handle that (catch it and use a default value? catch it and try to read another value? Let the exception propagate?)

With this change, you'll have to convert the integer value back to a string for the output:

print("The population will be "+str(populationCurrent)+".")

or using any of the various string formatting tools available. It's better to have populationCurrent as an integer, since there are more places in your code that assume it to be an integer than assume it to be a string.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

The only thing you need to do is convert populationCurrent from string to int:

populationCurrent = int(input("What is the current population?"))

The more concerning stuff is that your code doesn't do what it's supposed to: when x is 35 you will only have one birth, since 35 % 7 is 0, but no immigrant will arrive. Do something like this, removing the elif statements which do not make the code that more efficient anyway:

while x!=100:
    if (x%7==0):
        populationCurrent=populationCurrent+1
    if (x%13==0):
        populationCurrent=populationCurrent-1
    if (x%35==0):
        populationCurrent+=1
    x=x+1
print("The population will be ", populationCurrent, ".")

Though still note that the loop will stop after x gets to 100. You could reset it but I don't know for how long you want it to run.

RockOnRockOut
  • 751
  • 7
  • 18
0
def intInput(prompt):
    while 1:
        try: return int(input(prompt))
        except ValueError: print("Invalid Input!")

def YearToModifier(x):
    if x%35 ==0 or x%7 ==0: return 1
    if x%13 == 0: return -1
    return 0

populationCurrent = intInput("What is the current population?") #ensure you get an int
n_years = intInput("How Many Years?") #ensure you get an int
#in this case populationChange is independent of initial population (this is rarely the case in reality)
populationChange = sum(YearToModifier(x) for x in range(n_years)) 
#the population in the future is the initialPopulation + population Change ... duh...
populationFuture = populationCurrent + populationChange
print("The Population will be %d!"%populationFuture)

there you go

WRT @martjinpeters comment on OP you could change YearToModifier to

def YearToModifier(x):
    return sum([x%35 ==0,x%7 ==0,-1*int(x%13 == 0)])

of coarse as @AshokaLella points out you can calculate the total births/immigrations/deaths for a given number of years without actually visiting each year

births = n_years//7
immigrations = n_years//35
deaths = n_years//13
populationChange = births + immigrations - deaths
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Your method doesn't increase population by 2 every 35 seconds; there is a birth *and* an immigration, not either. And your method also fails to register both the death and birth at 91 seconds. – Martijn Pieters Sep 04 '14 at 17:22
  • Ah, your second version does do so. – Martijn Pieters Sep 04 '14 at 17:23
  • And please don't use recursion for input validation. A `while True` loop is far more efficient and won't lead to an infinite recursion error when a user tries to enter invalid data forever. See [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658) – Martijn Pieters Sep 04 '14 at 17:24