Skip to content
Sign upLog in
← Back to Community

How to: save your name in a variable

Profile icon
FoyjurRahman

I am a beginner and my teacher has told me to do some basic coding and I have forgotten how he did it. Can someone please help me out?

Answered by AphixDev [earned 5 cycles]
View Answer
Voters
Profile icon
GaelVillalobos1
Profile icon
FoyjurRahman
Comments
hotnewtop
Profile icon
AphixDev

Okay. So, you've done good so far. input() asks the thing in parentheses. Since you say name = input("Foyjur"), foyjur is the question asked. You might want to do something like this:

name = input("Enter your name: ") print("Welcome " + name)
Profile icon
FoyjurRahman

@nt998302
thank you very for your help. If it's alright with you I have some more questions to ask. I'm sorry if this causes you any inconvenience.

Profile icon
AphixDev

@FoyjurRahman
No, that's actually a very good thing. Ask me anything you need!

Profile icon
FoyjurRahman

@nt998302
Thanks for reassuring me by the way, I hate putting stress on people. But if you could explain to me the following and could you give examples of:

  1. Save your name in a variable
  2. Print a string along with your name using concatenation. Eg: Welcome Muhammad
  3. Ask for your age and store it in a variable
  4. Tell you your name is... and you are ... years old.
    Create a program that asks for your name and stores it in a variable.
    Then asks for your age and stores that in a variable too.
    After that, it should do a calculation and see how many years until you can drive.
    Then print the name and years left till you can drive.
    Now I kinda feel bad for asking you for this many favours but you don't have to do all of the things if you don't want to.
    My teacher doesn't explain the work properly for beginners like me so he just forgets about us and expects us to complete all the assignments he gives us.
    Thanks for the help.
Profile icon
AphixDev

@FoyjurRahman
I shall. Saving your name in a variable is easy.

name = input("what is your name? ")

Now you use the + operator to add two strings together in the print()

print("Welcome " + name)

Now, when someone inputs an age, you immediately cast it to an int

age = int(input("What is your age? "))

Then you add it all together. Remember to cast the age back to a string when using the + operator

print("Hello " + name + ". You are " + str(age) + " years old.")

And that's it!

Profile icon
AphixDev

@nt998302
For the driving part, you just need this

if age>=16: print("You can drive!") else: print("You can't drive")

This is a simple if/else statement. If your age is greater than or equal to 16, you can drive. And if that's not the case, you can't drive.

Profile icon
FoyjurRahman

@nt998302
I can't tell you how much I appreciate your help thank you very much.

Profile icon
FoyjurRahman

@nt998302
I wanted to ask, how would you share the work you have done to your teacher(online)?

Profile icon
AphixDev

@FoyjurRahman
The link you are on while editing your code is the link you send your teacher

Profile icon
FoyjurRahman

@nt998302
Thanks

Profile icon
FoyjurRahman

@nt998302
Could you show me how to create a program that calculates the circumference of a circle when the user gives it a radius?

Profile icon
AphixDev

@FoyjurRahman
ez

from math import pi inp = int(input("Radius: ")) answer = inp * 2 * pi print(f"Circumfrence: {answer}")
Profile icon
FoyjurRahman

@nt998302
thank you very much.

Profile icon
Coder39182

Youtube tutorials are quite good for basics. You can also look up stuff on google if you need some reference materials.