News Ticker

Menu

Nhập dữ liệu





<Python>Bài 5: Nhập dữ liệu
Ở bài này chúng ta tìm hiểu cách nhập dữ liệu từ bàn phím.


Tạo file: bai5.py

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")

print "So, you're %r old, %r tall and %r heavy." % (
    age, height, weight)

Câu lệnh raw_input("") để truyền giá trị nhập vào từ bàn phím cho biến.
Kết Quả:


from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

# argv là scipt (modun)
#sys là modun (thu vien)
# chuong trinh cho phep nhap 3 bien trong cmd bat buoc neu k nhap du 
se bao loi
# vd: *.py var1 var2 var3
Kết quả sẽ hiển thị ra những gì bạn nhập.

Chúc các bạn thành công!












Share This:

Post Tags:

No Comment to " Nhập dữ liệu "

  • To add an Emoticons Show Icons
  • To add code Use [pre]code here[/pre]
  • To add an Image Use [img]IMAGE-URL-HERE[/img]
  • To add Youtube video just paste a video link like http://www.youtube.com/watch?v=0x_gnfpL3RM