Python Multiplication Not Working
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.
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.
- jpalmer999
- Newbie
- Posts: 6
- Joined: Wed Aug 04, 2021 1:40 pm
- itch: jonathanpalmer
- Contact:
Python Multiplication Not Working
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.
-
- 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
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.
You need to turn your string into a number first to actually do some arithmetic on it.
Code: Select all
$ iCost = int(iQuantity) * 2
- jpalmer999
- Newbie
- Posts: 6
- Joined: Wed Aug 04, 2021 1:40 pm
- itch: jonathanpalmer
- Contact:
Re: Python Multiplication Not Working
Thank you so much! This had been driving me crazy yesterday.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
I love flight simming, coding, and writing in that order.
Who is online
Users browsing this forum: Majestic-12 [Bot], Ocelot