Tutorial Python untuk bukan pengatur cara/Mengira sehingga 10: Perbezaan antara semakan

Kandungan dihapus Kandungan ditambah
Aurora (bincang | sumb.)
Aurora (bincang | sumb.)
Baris 74:
 
== Contoh ==
'''Fibonacci.py'''
 
<source lang="python">
Fibonnacci.py
# This program calulatescalculates the fibonnacciFibonacci sequence
 
a = 0
#This program calulates the fibonnacci sequence
ab = 01
bcount = 10
countmax_count = 020
while count < max_count:
max_count = 20
while count = count <+ max_count:1
# we need to keep track of a since we change it
count = count + 1
old_a = a
#we need to keep track of a since we change it
old_aold_b = ab
old_ba = bold_b
ab = old_a + old_b
# Notice that the , at the end of a print statement keeps it
b = old_a + old_b
# from switching to a new line
#Notice that the , at the end of a print statement keeps it
b =print old_a + old_b,
# from switching to a new line
</source>
print old_a,
print
 
Output:
Baris 98 ⟶ 97:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
 
'''Password.py'''
<source lang="python">
# Waits until a password has been entered. Use controlControl-C to break out with outwithout
# the password
#Note that this must not be the password so that the
# while loop runs at least once.
password = "foobar"
# note that != means not equal
while password != "unicorn":
password = raw_input("Password: ")
print "Welcome in"
</source>
 
Cubaan menjalankan:
# Waits until a password has been entered. Use control-C to break out with out
Password: '''auo'''
# the password
Password: '''y22'''
#Note that this must not be the password so that the
Password: '''password'''
# while loop runs at least once.
Password: '''open sesame'''
password = "foobar"
Password: '''unicorn'''
#note that != means not equal
Welcome in
while password != "unicorn":
password = raw_input("Password:")
print "Welcome in"
 
== Latihan ==
Sample run:
 
Password:auo
Password:y22
Password:password
Password:open sesame
Password:unicorn
Welcome in