مثال۱۱: بازی حدس عدد بین 1 تا 10
import random rand=random.randint(1,10) number=int(input(' What number am I?? ')) if number==rand: print('you win!') elif number>rand: print('too high!') number=int(input('guess again: ')) if number==rand: print('you win...') else: print('you lose...') elif number<rand: print('too low!') number=int(input('guess again: ')) if number==rand: print('you win...') else: print('you lose...') else: print(' guess a number beetwin 1 to 10: ')
مثال12: بین A و Q یکی رو انتخاب کن!
import random me=random.choice(['A','Q']) guess=input('A or Q?? ').title() if guess == me: print('you win:)') else: print('bad luck:)') if me=='A': print('it was Arefe...') else: print('it was Queen...') ////////////////////////////////////////////////// import random me=random.choice(['A','Q']) guess=input('A or Q?? ').title() while True: if guess == me: print('you win:)') else: print('bad luck:)') guess=input('play again;) ') if guess=='end': break if me=='A': print('it was Arefe...') else: print('it was Queen...')
🐼 pandas in python:
🔰 پانداز پایتون یک کتابخانه منبع باز (open source) است که به طور گسترده برای تجزیه و تحلیل داده ها استفاده می شود.
📌کتابخانه Pandas برای خواندن و دستکاری داده ها در ML و علم داده استفاده می شود.
pip install pandasدستور بالا برای نصب کتابخانه Pandas در سیستم می باشد. #بخوانیم | #پایتون
مثال13: مقایسه پسورد...
pswd1=input('enter your password: ')
pswd2=input('enter it again: ')
if pswd1==pswd2:
print('WELCOME')
elif pswd1.lower()==pswd2.lower():
print('they MUST be the same case!')
else:
print('INCORRECT')مثال14: اگر پسوردی که کاربر وارد میکند صحیح بود، کاربر نام، سن و سایز کفش را برای چهار نفر از مشتریان خود وارد کند.
اگر مشخصات فردی را خواست نام فرد درون لیست را بپرسید و سن و سایز کفش آنها را نمایش دهید.
اگر کاربر میخواست مشخصات شخصی را که از لیست حذف کند، نام فرد را وارد کند. و این ردیف از داده ها حذف شود .
def customer():
list={}
for i in range(4):
name=input('NAME: ')
age=input('AGE: ')
shoe=input('SHOE SIZE: ')
list[name]={'Age':age,'Shoe Size':shoe}
while True:
option=input('1. View customer profile\n2. Remove customer profile\n3. Exit\n')
if option=='1':
ask=input('Enter name:')
print(list[ask])
elif option=='2':
remove=input('Enter name:')
del list[remove]
elif option=='3':
break
pswd1=input('enter your password: ')
pswd2=input('enter it again: ')
if pswd1==pswd2:
print('WELCOME')
customer()
elif pswd1.lower()==pswd2.lower():
print('they MUST be the same case!')
else:
print('INCORRECT')
آموزش پایتون
15.مثال پانزدهم
مثال15:
ساخت یک مسابقه ریاضی
اسم کاربر را بپرسد ،دو سوال تصادفی ایجاد کند و بپرسد
نام انها/نمره نهایی/سوال پرسیده شده/پاسخ انها را در یک فایل csv ذخیره کند
import csv
import random
score=0
name=input('what is your name? ')
q1_num1=random.randint(10,40)
q1_num2=random.randint(10,40)
question1=str(q1_num1)+ '+' +str(q1_num2)+ '='
ans1=int(input(question1))
realasn1=q1_num2+q1_num1
if realasn1==ans1:
score+=1
q2_num1=random.randint(10,40)
q2_num2=random.randint(10,40)
question2=str(q2_num1)+ '+' +str(q2_num2)+ '='
ans2=int(input(question2))
realasn2=q2_num2+q2_num1
if realasn2==ans2:
score+=1
file=open('quizscore.csv','a')
newrecord=name + ',' + question1 + ',' + str(ans1)+ ',' + question2 + ','+str(ans2)+ ','+str(score)+'\n'
file. write(str(newrecord))
file.close()