In [5]:
import random
number=random.randint(1,1000)
print(number)
In [12]:
import random,math
number=random.randint(1,1000)
print(math.log2(number))
print(math.ceil(math.log2(number)))
max_times=int(math.log2(number))
print(max_times)
In [14]:
i=0
while i<1000:
print("i=",i)
break
print("===========")
i=0
while i<1000:
i+=1
if i==100:
print("the i in if ==",i)
break
print("==========")
i=0
j=0
while i<5:
print('i=',i)
while j<100:
print("j=",j)
if j==2:
print("break")
break
j+=1
i+=1
In [21]:
import random,math
m=int(input('please enter m :'))
k=int(input('please enter k :'))
n=int(input('please enter n'))
total=0
i=0
while i<n:
i+=1
total+=random.randint(m,k)
print(math.sqrt(total/n))
In [1]:
import random,math
n=int(input('plz enter n: '))
m=int(input('plz enter m: '))
k=int(input('plz enter k: '))
total1=0
total2=0
i=0
while i<n:
temp=random.randint(m,k)
total1+=math.log(temp)
total2+=1/total1
i+=1
print(total1,total2,sep=' ')
In [11]:
import math,random
i=0
single=random.randint(1,9)
total=0
temp=0
n=int(input('plz enter n :'))
while i<n:
temp=(10*temp+single)
i+=1
print("tempt",temp)
total+=temp
print('total',total)
print('the toal number is : ',total)
In [1]:
import random, math
def win():
print("Haha, quite so easy! ")
def lose():
print("Hum...I bet I'll win the next time!")
def game_over():
print("See you~")
def show_team():
print('''programmed,designed and sponsored by: Wanqiong Xie
Thanks for my teacher though I don't know who he is(X)
Sending you times of kisses~(Actually I won't do that, I promiss)''')
def show_instruction():
print('''First enter the biggest number you may hold in your mind
Then I will guess a number and tell you what it is
If the number is smaller than the one you are thinking about, please tell me bigger
If the number is bigger than that, please tell me smaller
Otherwise please tell me bingo
Have a good time!''')
def menu():
print('''=====MENU=====
1. how to play
2. start game
3. quit
4. info
=====MENU=====''')
def guess_game():
n = int(input('Please enter the biggest number you may guess'))
max_times = math.ceil(math.log(n, 2))
chances = 0
a=1
b=n
while chances<=max_times:
guess=int((a+b)/2)
print(guess,", right?")
judge=input()
if judge=="smaller":
b=guess
elif judge=="bigger":
a=guess
else :
win()
break
chances+=1
if chances>max_times :
lose()
# 主函数
def main():
while True:
menu()
choice = int(input('Please enter your choice: '))
if choice == 1:
show_instruction()
elif choice == 2:
guess_game()
elif choice == 3:
game_over()
break
else:
show_team()
#主程序
if __name__ == '__main__':
main()
In [ ]: