Page 1 of 1

Python Multiplication Not Working

Posted: Sun Nov 28, 2021 10:43 pm
by jpalmer999

label Buy_Apple:
$ iQuantity = renpy.input("How many apples do you want?")
$ iQuantity = iQuantity.strip() or 1
$ iCost = iQuantity * 2

menu:
"This will cost you [iCost] gold for [iQuantity] apples."


So as far as I'm aware the code should output "This will cost you 4 gold for 2 apples." However, the result that is occurring is "This will cost you 22 gold for 2 apples." I understand what is happening, it is interpretting iCost = iQuantity * 2 as just putting it there another time. I'm getting a bit confused as I've never seen this occur before in programming.

Re: Python Multiplication Not Working

Posted: Mon Nov 29, 2021 2:10 am
by enaielei
You can multiply strings in python, to repeat the same string many times (e.g. "abc" * 3 == "abcabcabc"). That's what happens here.
You need to turn your string into a number first to actually do some arithmetic on it.

Code: Select all

$ iCost = int(iQuantity) * 2

Re: Python Multiplication Not Working

Posted: Mon Nov 29, 2021 6:34 am
by jpalmer999
enaielei wrote: Mon Nov 29, 2021 2:10 am You can multiply strings in python, to repeat the same string many times (e.g. "abc" * 3 == "abcabcabc"). That's what happens here.
You need to turn your string into a number first to actually do some arithmetic on it.

Code: Select all

$ iCost = int(iQuantity) * 2
Thank you so much! This had been driving me crazy yesterday.