-1

The problem is that I create a variant (let's say lemon_number = 5) and I want to multiply it by the age of the person (that I asked with input()). I tried int, str and didn't find anything.

There's the code:

print("Hi. Today we will calculate your age with your shoes size.")
response1 = input("Are you ready ? (1 = yes, 2 = no) ")
no = 2
yes = 1
if response1 == str(no):
    print("ok")
elif response1 == str(yes):
    five: int = 5
    shoes_size = input("Good ! So firstly tell me your shoes size : ")
    print("Now, we have to multiplie it by 5. It gives " + ((str)(shoes_size)*(int)(five)))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Furelkan
  • 9
  • 2
  • 3
    Welcome to StackOverflow! What do you think the expression `(str)(shoes_size)*(int)(five)` evaluates to? Do you know what `"fred" * 5` evaluates to? Also note that `(var1)(var2)` simply evaluates as `var1(var2)`. There are no casts in Python, only function calls. – Brian61354270 Jul 23 '20 at 14:23
  • 2
    Since this is python. try executing this line by line in a python console. You will find it useful to see what each line does. You can open a python console by typing python in your terminal. Or use an online python console. – leoOrion Jul 23 '20 at 14:24
  • Python does not have C-style casts. `int` is a function, so write `int(shoes_size)` to convert it to an int. – alexis Jul 23 '20 at 14:27
  • This style is C++ I guess. – Pygirl Jul 23 '20 at 14:32

4 Answers4

1

Try replacing the final call to print with:

print("Now, we have to multiplie it by 5. It gives " + str(int(shoes_size)*five))

Your output will then become

Good ! So firstly tell me your shoes size : 2
Now, we have to multiplie it by 5. It gives 10

Note that the result is 10 now.

You need to convert input into integer int(shoes_size), do calculation (multiplication) and the result convert to str before concatenating it with string.

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
ipj
  • 3,488
  • 1
  • 14
  • 18
  • 1
    Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Jul 23 '20 at 14:27
  • Thanks. Hope it'll be me helping you one day :D – Furelkan Jul 23 '20 at 14:41
1

You seemed to be in the correct direction. The problem is that in the last print statement you are multiplying a string by an int.

The way string multiplication works in python is that if we multiple 'a' by 3 you will get this result:

'a' * 3
>>>'aaa'

The answer to your problem would be to cast shoe size to an int and then cast the result to a string.

print("Now, we have to multiplie it by 5. It gives " + str(((int)(shoes_size)*(int)(five))))

This isn't the ideal way to do it, but it will work for your problem

Alex
  • 151
  • 8
  • 1
    Hello! As noted in the top level comments, there is no such thing as a "cast" in Python. The syntax in your second snippet is rather misleading for beginners. It's also rather meaningless to call the builtin `int` on an existing integer object. – Brian61354270 Jul 23 '20 at 14:29
  • @Brian I think you are correct. However I did want to give an answer the resembled the user code as close as possible. I did note in my solution that it is not the ideal way to go about the problem. – Alex Jul 23 '20 at 14:31
0

Try it this way:

response1 = int(input("Are you ready ? (1 = yes, 2 = no) "))
if response1 == 2:
    print("ok")
elif response1 == 1:
    shoe_size =int(input("Good ! So firstly tell me your shoes size : "))
    print("Now, we have to multiplie it by 5. It gives " + shoe_size*5)
a1426
  • 256
  • 2
  • 8
0

Bit late here, but this one just adds some short explanations and links for some edits.

print("Hi. Today we will calculate your age with your shoes size.")
response1 = input("Are you ready ? (1 = yes, 2 = no)")

#Using variables that are only used once is generally not a good idea.
#(Code in question: no = 2, yes = 1)
if response1 == "2":
    print("ok")
elif response1 == "1":
    # Code I am talking about: five: int = 5
    #(There is no need to hint whether a variable in python is an integer or a string). The type hint (five :int) is used when you want others reading your code to understand how it works(like if you are a developer/module publisher).
# ^ see my link at the end for more information on type hints
    #Also, "five" was only used once. The purpose of variables is to be reused.
    shoes_size = input("Good ! So firstly tell me your shoes size : ")
    #The way you convert something's type is by calling int(something).
    print("Now, we have to multiply it by 5. It gives " + int(shoes_size)*5)

TL;DR: Don't use "only-use" variables. For example, if you want to print(1), don't

a = 1
print(a)

The simplest way to convert the type of something is by calling int(something),str(something),float(something),etc. not (int)(something)` Saves you a pair of parentheses, and makes the code simpler. Finally, multiplying a string by a number repeats it that many times. The full list of interactions is visible at this link, but be warned, it is the docs, so it is pretty technical.

link about type hints

a1426
  • 256
  • 2
  • 8
  • thanks much :D And I complicate things because i'm just learning and these are exercices. So I do things to learn more but, you explained well, thanks ^^ – Furelkan Jul 23 '20 at 18:20