Search code examples
pythoninputsyntax-erroruser-input

Weight python mistake SyntaxError


I'm reviewing Python again and I came to this early stage where I'm stuck again. I need to:

Write a program in Python that will ask for the user's
Name and 'weight' in kilograms. The program will then
Output a sentence informing the user of their weight in grams and in pounds.

This is my code:

myName = str(input('What is your name?:'))

myWeight = int(input( myName+ 'What is your weight in kilograms?:')

weightGrams = 'myWeight*1000'
weightPounds = 'myWeight*2.2'



print(myName+ 'You would weigh', weightGrams, 'in grams and' weightPounds, 'in pounds!')

I am not getting the correct output and also I am getting SyntaxErrors. The SyntaxErrors are here:

weightGrams = 'myWeight*1000'
weightPounds = 'myWeight*2.2'

And at the end:

print(myName+ 'You would weigh', weightGrams, 'in grams and' weightPounds, 'in pounds!')

How can I fix those?


Solution

  • Don't treat int as string:

    weightGrams = myWeight*1000
    weightPounds = myWeight*2.2
    

    Also you have syntax errors in your print:

    print(myName+ ' You would weigh', weightGrams, 'in grams and', weightPounds, 'in pounds!')
    

    In addition you don't need the cast to str here since input already gives out a string:

    myName = input('What is your name?:')
    

    Final edit: you are missing a closing bracket ) here:

    myWeight = int(input( myName+ 'What is your weight in kilograms?:'))