簡體   English   中英

在Ubuntu Linux上運行python文件

[英]Run a python file on Ubuntu Linux

每當我嘗試在終端中運行我的python代碼時,我總會得到這樣的東西,

Hello World
Enter your name: Tyler
Traceback (most recent call last):
File "HelloWorld.py", line 3, in <module>
name = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'Tyler' is not defined

我是Python的新手所以請原諒我,我通常用c#編程,但是Windows崩潰了所以我正在嘗試學習python。

這是我的代碼:

print('Hello World')

name = input('Enter your name: ')

print('Hi', name)

age = input('Enter your age: ')

age = int(age)

if (age == 35):
    print('You are as old as Derek Banas')
if (age == 19):
    print('You are the same age as me!')
else:
    print('You are a different age than me')

print('Hello', name, 'You are', age, "It's nice to see you again!")

你似乎使用python 3所以從終端運行它你需要使用python3 script.py而不是python script.py

您應該使用raw_input而不是input

謝謝大家對我的耐心,這是我根據每個人輸入找到的解決方案! 我使用Python 3.4在IDLE中編碼,然后在終端中輸入python。 當我將其更改為raw_input時,它修復了它,但在IDLE中運行它時給了我一個錯誤。

這是我找到的解決方案:當我使用cd Desktop / Python然后從那里運行它我應該使用Python3而不是python所以代碼將如下所示:

cd Desktop/Python
python3 HelloWorld.py

這解決了我的問題! 謝謝大家的幫助!

name = input('Enter your name: ')
age = input('Enter your age: ')

改為使用

name = raw_input('Enter your name: ')
age = raw_input('Enter your age: ')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM