简体   繁体   中英

How can I make PyCharm run my whole code?

I have to write a code that prompts the user for their name and age, and then tells them the year they were born. My code in PyCharm is correct and has no errors, but when I run my program it only outputs the first line of my code (which is 'What is your name?'). I am really new to coding and PyCharm, so I wouldn't doubt that I've done something wrong with setting up PyCharm, I just don't know what that would be. I've tried uninstalling and reinstalling and I've followed tutorials given by my professor. It isn't just this specific code either, I have tried several codes and every time it only outputs the first line of code. This is my code:

user_name = input('What is your name?')
user_age = int(input('How old are you?'))

print('Hello {}! You were born in {}.'.format(user_name, int(2021 - user_age)))

enter image description here

enter image description here

**Editing to add screenshots of my program and run configurations

If your code is this:

user_name = input('What is your name?') user_age = int(input('How old are you?'))
print('Hello {}! You were born in {}.'.format(user_name, int(2021 - user_age))) 

the problem is simple, just add a new line like this:

user_name = input('What is your name?') 
user_age = int(input('How old are you?'))
print('Hello {}! You were born in {}.'.format(user_name, int(2021 - user_age))) 

Hope this solves your question. Happy coding.

It's because you're using input() which only fully executes when you give it an input, for example:

name = input("name: ")
age = int(input("age: "))
print(name, age)

When I run this code I get this:

在此处输入图像描述

Until I type in my name, or any string for that matter, and press enter, the rest of my script won't run;

在此处输入图像描述

Once both inputs have been satisfied, the print will display the text:

在此处输入图像描述

Hope that answers your question:)

EDIT: I'm assuming you didn't write:

 user_name = input('What is your name?') user_age = int(input('How old are you?'))

In one line since that would raise a Syntax Error. If you did, just change it to:

user_name = input('What is your name?') 
user_age = int(input('How old are you?'))

Which should fix it.

Want to add to answer by DiegoP Apparently lines breaks or simply 'enter button' is important in python. Your code runs line by line, hence you should write each line in a different line (makes sense?)

Also, you don't to define int two times, once an int, it will always be an int in your code unless you specifically want to change it to other datatype.

So the last line can be simply:

print('Hello {}! You were born in {}.'.format(user_name, (2021 - user_age)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM