Python Multiplication Not Working

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
jpalmer999
Newbie
Posts: 6
Joined: Wed Aug 04, 2021 1:40 pm
itch: jonathanpalmer
Contact:

Python Multiplication Not Working

#1 Post 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.
I love flight simming, coding, and writing in that order.

enaielei
Veteran
Posts: 293
Joined: Fri Sep 17, 2021 2:09 am
Organization: enaielei
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Python Multiplication Not Working

#2 Post 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

User avatar
jpalmer999
Newbie
Posts: 6
Joined: Wed Aug 04, 2021 1:40 pm
itch: jonathanpalmer
Contact:

Re: Python Multiplication Not Working

#3 Post 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.
I love flight simming, coding, and writing in that order.

Post Reply

Who is online

Users browsing this forum: Kocker