这是我的,稍微做了更多的改进,添加了:告知玩家剩余次数,根据不同猜测次数有不同的结果,如绝杀吃鸡。^_^ import random secret = random.randint(1,2) print('------------我爱打游戏------------') temp = input ("不妨猜一下我在想哪一个1-10以内的整数(你有四次机会)?") guess = int(temp) time = 0 count = 4 while guess == secret and time == 0: print("这么叼的吗?一次性就猜对了!") time = time + 20 count = count - 1 while guess > secret and time < 1: temp = input ("傻逼,猜大了,晓得不?你还有%d次机会 再猜:"%(count-1)) guess = int(temp) time = time + 1 count = count - 1 while guess < secret and time < 1: temp = input("傻逼,猜小了,你还有%d次机会 再猜:"%(count-1)) guess = int(temp) time = time + 1 count = count - 1 while guess == secret and time < 2: print("厉害了,猜第二次就对了哈") print("哼,猜中了也不会给你什么奖励,你来打我呀?!^_^") time = time + 20 count = count - 1 while guess != secret and time secret: print("你怕不是个傻子哦?!大了大了") temp = input("真的是,怎么没猜对呢?你还有%d次机会,再猜:"%(count)) guess = int(temp) time = time + 1 count = count - 1 else: if guess < secret: print("小了小了,智障") count = count - 1 temp = input("真的是,怎么没猜对呢?你还有%d次机会,再猜:"%(count)) guess = int(temp) time = time + 1 while guess == secret and time = 4: print("智商低就不要玩这游戏,这游戏不适合你,回家种地去吧!傻逼!!!!!!") print("剩余次数=%d"%(count)) if count >= 3 and time >= 20: print("牛逼啊,真的是我的蛔虫,我自己都不造是多少!") else: if count == 0 and time >= 20: print("还行吧!绝杀!大吉大利,晚上吃鸡!") print ("Game Over ")
想不到and可以怎麼用 不過設了一個variable " i "也可以做一個有限迴圈 暫時想不到可以更簡短了 import random m=random.randint(0,10) x=input("輸入") n=int(x) i=0 while n != m: i=i+1 if i==3: print("boo") break if n>m: print("smaller") if n
我也贴一个我的,但是感觉有点复杂,等学到最后应该可以再简化: import random secret = random.randint(1,10) print ('Let\'s Play a Game: ') print ('You only have 3 times to guess, so BE CAREFUL!!!') time = 3 temp = input('Guess a Number Between 1-10:') guess = int(temp) if guess == secret: print ('Bingo!') print ('But no rewards.....') else: if guess > secret: print ('Bigger') else: print ('Smaller') time = time - 1 print (str(time) + ' times left') while (guess != secret) and (time > 0): temp = input('Please Try Again:') guess = int(temp) if guess == secret: print ('Bingo!') print ('But no rewards.....') else: if guess > secret: print ('Bigger') else: print ('Smaller') time = time - 1 if time!= 0: print (str(time) + ' times left.') if time == 0: print ('No chances left and the answer is '+str(secret)) print ('Game Over')
贴一个我的,仅供参考 import random secret = random.randint(1,10) print('Start the game!') print('You have three chances to win!') temp = input ('Please guess the number: ') time = 3 guess = int(temp) while (guess != secret and time>1): if guess > secret: print ("Sorry, you are wrong. The number should be smaller.") else: print ("Sorry, you are wrong. The number should be bigger.") time = time -1 print('Watch out! The remianing times you have: ' +str(time) ) temp = input ('Please guess the number again: ') guess = int(temp) if guess == secret: print("Correct!") print("You win! But no reward lol...") print("End of the game.") else: print ('Run out 3 times! Answer is: '+str(secret)) print ('Game Over!')
import random ans = random.randint(1,100) t=1 temp = input('input your number') number = int(temp) while number != ans and t3: print ('no remaining chance,answer is %s'%ans) else: temp = input('input your number again') number = int(temp) if number == ans: print ('good job') print ('finish')
实现了随机取数字并且三次机会,仅供参考~ import random #this is the first comments se=random.randint(1,10) times=3 n1=0 while (n1!=se) and (times!=0): n=input('Again name a number') while n.isdigit()==0: n=input('Opps, you have to input a number, Please input a number') n1=int(n) if n1==se: print('bingo') else: if n1>se: print('too large, take a smaller one') else: print('too small, try a larger guess') times=times-1 print('remaining chances to guess is ',times, 'times') print('game over')
显示这个错误是什么意思啊,有谁知道吗?求大神解答下Traceback (most recent call last): File "E:/python/新建文件夹/001.py", line 14, in time=time+1 NameError: name 'time' is not defined
试着瞎写了一个: import random secret = random.randint(1,10) print('=====My first Python Work=====') temp=input("Please input a number, you have 5 chances left:") guess=int(temp) t=1 if guess==secret: print("Congratulations! You are right.") else: if guess>secret: print("Sorry, it's too large! You have",5-t,"chances left:") else: print("Sorry, it's too small! You have",5-t,"chances left:") while guess!=secret: t=t+1 if (5-t)>=0: temp=input("Plese input a number:") guess=int(temp) if guess==secret: print("Congratulations! You are right.") else: if guess>secret: print("Sorry, it's too large! You have",5-t,"chances left:") else: print("Sorry, it's too small! You have",5-t,"chances left:") else: print("No chances anymore!") break print("==========GAME OVER==========")
import random ans = random.randint(1, 10) temp = input('guesss what I am thinking: ') guess = int(temp) n = 0 while (guess != ans) & (n < 2): n = n + 1 if guess > ans: guess = int(input('bigger, please try again: ')) else: guess = int(input('smaller, please try again: ')) if guess == ans: print('correct! Game is over now') else: print('already 3 times, GG, ans is:', str(ans))
elif Statements The elif (short for else if) statement is a shortcut to use when chaining if and else statements. A series of if elif statements can have a final else block, which is called if none of the if or elif expressions is True. For example: num = 7 if num == 5: print("Number is 5") elif num == 11: print("Number is 11") elif num == 7: print("Number is 7") else: print("Number isn't 5, 11 or 7") Result: >>> Number is 7 >>>
贴一个我的答案 import random secret=random.randint(1,10)#生成1到10的整型随机数 print("-----------------Guess number game------------------") temp=input("Please input a number,you have 3 chances in total:") number=int(temp) chance = 2 if number==secret: print("awesome,you got the anwser 1st time") print("congratulations") else: while chance>0 or number==secret: if numbersecret: print("too big, " + "you only have " + str(chance) + " chance now") else : print("congratulations") break chance = chance - 1 temp = input("Please input a new number:") number = int(temp) print("Game Over")
import random n = random.randint(1,10) chance = 3 print ('guess a number from 1 to 10, you have just 3 times to guess') antwort = int(input('enter your number:')) while antwort != n and chance > 1: if antwort > n: print ('Falsch, zu gross') else: print ('Falsch, zu wenig') chance = chance - 1 print ('your chance left',chance) antwort = int(input('enter your number again:'))
if antwort == n: print ("richtig") else: print ('your chance has been wasted. The right one is', n) 有一个问题,就是为什么chance 变成1了之后还是能进入whlie 循环?
secret = random.randint(1,20) print("-----------------------game-----------------") temp = input("guesses one number:") guess = int(temp) ti = 1 while (guess != secret) and (ti secret: print("wow, too large") print("you need change your number smaller!") else: print("wow, too small") print("you need change your number larger!") temp = input(" guesses another number: ") guess = int(temp) ti =ti + 1 print("chance left:" +str(3-ti)) if guess == secret: print("fuck, you are right you got nothing eventhought you are right! game over~~~") else: print("ohou, no chance any moer") print(secret)
贴一个我的,仅供参考
print('猜数字游戏,你只有三次机会,加油哦')
import random
secret = random.randint(1,10)
temp = input('请输入一个整数:')
time = 3
guess = int(temp)
while(guess != secret and time >0):
if guess > secret:
print('猜错了,大了')
else:
print('猜错了,小了')
time = time - 1
temp = input('请重新输入:')
guess = int(temp)
print('小心,你的剩余机会只有:'+str(time))
if guess == secret:
print('回答正确了,游戏结束')
else:
print('3次机会用完你输了!!!正确答案是:'+str(secret))
真的讲的好赞! 作为一个编程小白~真的好喜欢你的讲课风格
謝謝你的video "這裏可以抄一下” temp = input("你知道我在想什麽數字嗎:")
guess = int(temp)
while guess !=8:
temp = input("哎呀,錯了,請重新輸入吧:")
guess = int(temp)
if guess == 8:
print("你會心靈感應嗎?")
print("哼,中了也沒有獎勵")
else:
if guess > 8:
print("哥,大了大了~~~~~~~")
else:
print("嘿,小了,小了~~~~~~~")
print("游戲結束,不玩了^_^")
yhyh YT 想問一下如果一開始就輸入8
不就會被 while guess !=8: 這行判定條件不符合
直接到 print("游戲結束,不玩了^_^") 這行嗎
看不太懂為什麼小甲魚也是這樣打
也贴一个我的解答吧,有需要的可以参考
描述:完成了课后练习的所有要求,即:若第一次答错,也可收到提示(猜大了还是猜小了);连续猜的次数有限制(可设置,程序预设为3次);答题结束(无论对错),都会在最后给出正确答案。
import random
secret = random.randint(1,10)
time = 1
print ("-------Game Start--------")
temp = input("choose a number from 1 to 10:")
guess = int(temp)
if guess == secret:
print("bingo!")
else:
if guess > secret:
print("Wrong, more")
else:
print("Wrong, less")
temp = input("try again:")
guess = int(temp)
time = time + 1
while (guess != secret and time < 3):
if guess > secret:
print("more")
else:
print("less")
temp = input("try more:")
guess = int(temp)
time = time + 1
if guess == secret:
print("bingo")
else:
print("you lose! X"+str(time)+"!!!")
print("the right ansewer is "+ str(secret))
print("-------Game Over--------")
if guess>secret,不应该是print less吗,如果猜的大了 应该说小才对,我是新手 请多指教
@@andreali1912 print more 指的是 输入的输太大了。 而不是指示你往小里猜。英语问题
贴一个我的课后练习,可以参考一下
import random
secret = random.randint(1,10)
print('----------------我爱鱼C工作室------------------')
print("猜数字游戏,你只有三次机会,加油哦!")
count = 0
while count != 3:
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
guess = int(temp)
count = count + 1
if guess == secret:
print("我靠,你是小甲鱼心里的蛔虫吗?")
print("哼,猜中了也没奖励!")
break
else:
if guess > secret:
print("哥,大了大了~~")
else:
print("嗨,小了小了!!")
print("游戏结束,不玩啦^_^")
print('小甲鱼工作室')
import random
secret=random.randint(0,10)
print("你一共有四次机会")
time=4
temp = input("请输入数字:")
guess = int(temp)
while guess!=secret:
if guess>secret:
print("大了大了")
elif guess 1:
time = time - 1
chancetime = str(time)
print("猜错了,你还有" + chancetime + "次机会")
temp = input("重新输入吧:")
guess = int(temp)
else:
print("次数用完了")
break
else:
print("恭喜你答对了")
print("游戏结束")
我这个虽然是可以用的,但还是有一个问题,答对的时候我必须用一个while从句以外的else来print 恭喜答对了,不然就程序结束直接跳出去了,但是我看视频小鱼把恭喜答对的内容放在while 从句中,当guess==secret的时候仍然能打印,但我的就直接跳出循环了,包括小鱼第一次录入5的时候也直接跳出循环了,但第二次就没有,真是奇怪啊,是因为赋值的位置吗?小鱼是之前赋值的,我是在之后赋值的。
import random
secret = random.randint(1,10)
temp = input('猜一个数字:')
guess = int(temp)
while guess != secret:
if guess > secret:
print('大了')
else:
if guess < secret:
print('小了')
temp = input('请重新输入:')
guess = int(temp)
if guess == secret:
print('对了')
print('结束')
##我的改进:第一次猜对就会提示对了,然后结束。第一次猜错会讲大了还是小了,然后继续直到猜对为止。
##以前从来没接触过编程,第一次学习,希望有所收获。
贴一个我的,次数限制的设定time是参考nando
的,
同时还加上了自我设定次数的功能,因为0在py里面也算有效值,所以在实际运行中要-1,
还加上了第一次就猜中与后几次猜中有不同的print语句,供大家参考:
print("猜数字------by Joe")
import random
secret = random.randint(1,10)
cishu = input("你这么厉害,说话又这么好听,你打算猜多少次:")
time = int(cishu)-1
temp = input("OK!那么来猜一下Joe心里在想着哪个数字,给你"+str(time+1)+"次机会:")
guess = int(temp)
while (guess !=secret and time >0):
if guess > secret:
print("老哥,大了大了!")
else:
print("嘿,小了小了!")
time = time - 1
print("小心,你只有"+str(time+1)+"次机会了")
temp = input("请重新输入吧:")
guess = int(temp)
if (guess == secret and time == int(cishu)-1):
print("凹槽!第一次就答对了?你是Joe肚子里面的蛔虫嘛?
回答正确!游戏结束。")
else:
if (guess == secret and time < int(cishu)-1):
print("终于答对咯!游戏结束。")
else:
print("机会都用完咯!Game Over,你输了,嘿嘿~")
用 for loop和break设计的代码如下:
print('--------我爱C鱼工作室内---------')
for i in range(5):
temp = input("游戏重新开始囖--不妨猜一下小现在心里想的是哪个数字:")
guess = int(temp)
if guess == 8:
print('你牛啊,对啊你猜对啦 你是能进入他的脑子')
print('中文人都知道 正常 没奖励')
break
elif guess > 8:
print('猜错啦 猜大了')
else:
print('猜小啦')
print('不玩啦 game over')
有个问题啊,就是第一次修改可以无限循环以后为什么run以后第一次输数字只会显示错了,而不会显示大了还是小了,看视频16:10
这是我的,稍微做了更多的改进,添加了:告知玩家剩余次数,根据不同猜测次数有不同的结果,如绝杀吃鸡。^_^
import random
secret = random.randint(1,2)
print('------------我爱打游戏------------')
temp = input ("不妨猜一下我在想哪一个1-10以内的整数(你有四次机会)?")
guess = int(temp)
time = 0
count = 4
while guess == secret and time == 0:
print("这么叼的吗?一次性就猜对了!")
time = time + 20
count = count - 1
while guess > secret and time < 1:
temp = input ("傻逼,猜大了,晓得不?你还有%d次机会 再猜:"%(count-1))
guess = int(temp)
time = time + 1
count = count - 1
while guess < secret and time < 1:
temp = input("傻逼,猜小了,你还有%d次机会 再猜:"%(count-1))
guess = int(temp)
time = time + 1
count = count - 1
while guess == secret and time < 2:
print("厉害了,猜第二次就对了哈")
print("哼,猜中了也不会给你什么奖励,你来打我呀?!^_^")
time = time + 20
count = count - 1
while guess != secret and time secret:
print("你怕不是个傻子哦?!大了大了")
temp = input("真的是,怎么没猜对呢?你还有%d次机会,再猜:"%(count))
guess = int(temp)
time = time + 1
count = count - 1
else:
if guess < secret:
print("小了小了,智障")
count = count - 1
temp = input("真的是,怎么没猜对呢?你还有%d次机会,再猜:"%(count))
guess = int(temp)
time = time + 1
while guess == secret and time = 4:
print("智商低就不要玩这游戏,这游戏不适合你,回家种地去吧!傻逼!!!!!!")
print("剩余次数=%d"%(count))
if count >= 3 and time >= 20:
print("牛逼啊,真的是我的蛔虫,我自己都不造是多少!")
else:
if count == 0 and time >= 20:
print("还行吧!绝杀!大吉大利,晚上吃鸡!")
print ("Game Over ")
想不到and可以怎麼用
不過設了一個variable " i "也可以做一個有限迴圈
暫時想不到可以更簡短了
import random
m=random.randint(0,10)
x=input("輸入")
n=int(x)
i=0
while n != m:
i=i+1
if i==3:
print("boo")
break
if n>m:
print("smaller")
if n
temp = input("你猜我心中的数字是多少,你只有三次机会:")
guess = int(temp)
nuber = 3
while (temp != 8) and (nuber>1):
nuber = nuber -1
answer = input("错啦,重新猜一下:")
guess = int(answer)
if guess == 8:
print("我草牛逼")
else:
if guess > 8:
print("大了")
else:
print("小了")
print("游戏结束!")
我也贴一个我的,但是感觉有点复杂,等学到最后应该可以再简化:
import random
secret = random.randint(1,10)
print ('Let\'s Play a Game: ')
print ('You only have 3 times to guess, so BE CAREFUL!!!')
time = 3
temp = input('Guess a Number Between 1-10:')
guess = int(temp)
if guess == secret:
print ('Bingo!')
print ('But no rewards.....')
else:
if guess > secret:
print ('Bigger')
else:
print ('Smaller')
time = time - 1
print (str(time) + ' times left')
while (guess != secret) and (time > 0):
temp = input('Please Try Again:')
guess = int(temp)
if guess == secret:
print ('Bingo!')
print ('But no rewards.....')
else:
if guess > secret:
print ('Bigger')
else:
print ('Smaller')
time = time - 1
if time!= 0:
print (str(time) + ' times left.')
if time == 0:
print ('No chances left and the answer is '+str(secret))
print ('Game Over')
temp = input("你猜我心中的数字是多少,你只有三次机会:")
guess = int(temp)
nuber = 3
while (temp != 8) and (nuber>1):
nuber = nuber -1
answer = input("错啦,重新猜一下:")
guess = int(answer)
if guess == 8:
print("我草牛逼")
else:
if guess > 8:
print("大了")
else:
print("小了")
print("游戏结束!")
讲的东西很喜欢,只是英文发音让我醉了!
16:25 秒那裏有點不懂,為甚麼第一次輸入錯誤時,並沒有提示是小了還是大了?
因为直接执行temp = input ("不妨猜一下小甲鱼心里想的数字:"),需要把判断大小的指令放到这条前面,可参考置顶评论。
'''
课后作业
'''
import random
answer = random.randint(1,20)
temp = input("你猜我心中的数字是多少,友情提醒20以内,你只有5次机会:")
guess = int(temp)
nuber = 5
while (guess != answer) and (nuber > 1):
nuber = nuber - 1
if guess > answer:
temp = input("大啦,重新猜一下:")
guess = int(temp)
else:
temp = input("小啦,重新猜一下:")
guess = int(temp)
if guess == answer:
print("牛逼,猜对了!")
else:
print("你没有机会了,游戏结束!")
这个答案也可以实现,但是总感觉不够简洁,求大神指导怎么优化
print('小甲鱼工作室')
import random
secret=random.randint(0,100)
print("你一共有四次机会")
time=3
temp = input("请输入数字:")
guess=int(temp)
if guess==secret:
print("答对了")
elif guesssecret:
print("大了大了")
elif guess 1:
time = time - 1
chancetime = str(time)
print("猜错了,你还有" + chancetime + "次机会")
else:
print("次数用完了")
break
print("游戏结束")
贴一个我的
temp=input("来猜一个数字吧:")
guess=int(temp)
time=1
if guess==8:
print("卧槽,你是我心里的蛔虫吗??")
print("哈哈")
while guess!=8 and time8:
print("哎呀,大了,大了!")
else:
print("哎呀,小了,小了!")
temp=input("请重新输入一个数字吧:")
guess=int(temp)
time=time+1
if guess==8:
print("卧槽,你是我心里的蛔虫吗??")
print("哈哈")
print("游戏结束,不玩啦!")
code = input("请输入密码:")
guess = code # 注意如果密码中带有数字以外的字符,code前面不能加int()
times = 3
if guess == "Wx760531": # 注意如果密码是字符串需要加“”或‘’
print("密码正确")
print("还要多练习")
else:
while guess != 'Wx760531' and times > 0:
code = input("密码错误,请重新输入密码:")
guess = code
if guess == 'Wx760531':
print("密码正确")
print("还要多练习")
else:
print("密码错误,再尝试%s次程序将关闭"%(times-1))
times = times - 1
import random
secret = random.randint (1,10)
times = 3
print ('请输入1~10中任意一个整数,您一共有三次机会')
number = input("请输入数字")
guess = int(number)
while guess != secret and times > 1:
if guess < secret:
print ('数字小了')
times = times-1
number = input ('请重新输入,您还有' + str(times) +'次机会')
guess = int(number)
else:
print ('数字大了')
times = times-1
number = input ('请重新输入,您还有' + str(times) +'次机会')
guess = int(number)
if guess == secret:
print ('恭喜您,回答正确')
else:
print('回答次数已用完,游戏结束!正确答案是'+str(secret))
試著改寫了一下
import random
ans = random.randint(1,100)
print('猜數字小遊戲')
num_1 = input('請輸入1-100的數字 :')
num_1 = int(num_1)
min_num = 1
max_num = 100
n=0
while num_1 < 1 or num_1 > 100 :
print('數字太大或太小,請重新輸入')
num_1 = input('請重新輸入1-100的數字')
while num_1 >= 1 and num_1 ans:
if num_1 < max_num:
max_num = num_1
print('請再輸入介於',min_num,'-',max_num,'的數字')
num_1 = input()
num_1 = int(num_1)
n=n+1
elif num_1 < ans:
if num_1 > min_num:
min_num = num_1
print('請再輸入介於',min_num,'-',max_num,'的數字')
num_1 = input()
num_1 = int(num_1)
n=n+1
else:
if n ==0:
print('太強了吧一猜就中')
n=n+1
else:
break
print('遊戲結束囉')
print('答案是:',ans)
print('共猜了',n,'次')
if n < 3:
print('3次內猜中,有點強喔')
elif n >=3 and n
贴一个我的,仅供参考
import random
secret = random.randint(1,10)
print('Start the game!')
print('You have three chances to win!')
temp = input ('Please guess the number: ')
time = 3
guess = int(temp)
while (guess != secret and time>1):
if guess > secret:
print ("Sorry, you are wrong. The number should be smaller.")
else:
print ("Sorry, you are wrong. The number should be bigger.")
time = time -1
print('Watch out! The remianing times you have: ' +str(time) )
temp = input ('Please guess the number again: ')
guess = int(temp)
if guess == secret:
print("Correct!")
print("You win! But no reward lol...")
print("End of the game.")
else:
print ('Run out 3 times! Answer is: '+str(secret))
print ('Game Over!')
贴一个自己做的一个,加了一些功能,供大家参考:
1.可以在程序运行时设置最大尝试次数
2.显示当前正确答案所处的范围
3.可以连续游戏,不需要猜出答案后重启,使用字母输入结束游戏
4.超出规定次数,游戏结束,显示正确答案
5.显示当前已经猜测的次数
import random
min_num = 0
max_num = 10000
secret = random.randint(min_num, max_num)
step = 0
current_min = min_num
current_max = max_num
max_step = 4
print("===============欢迎来到猜数字游戏!===============")
print("********您可以在输入答案时输入E退出本程序********
")
def ResetGame():
global step
global current_min
global current_max
global max_step
global secret
step = 0
current_min = min_num
current_max = max_num
secret = random.randint(min_num, max_num)
max_step = int(input("请输入最大尝试次数:"))
ResetGame()
while True:
if step >= max_step:
print("已经达到最大尝试次数!游戏失败,将重新开始!")
print("正确答案为:", secret)
print("------------------------")
ResetGame()
step += 1
answer = 0
input_string = input ("Step:{2},请输出您的答案,范围在({0}->{1}):".format(current_min, current_max, step))
# Check Exit
if input_string == 'E':
print("再见!欢迎下次再来!")
break;
else:
answer = int(input_string)
# Check your answer
if answer > secret:
print ("您的答案大了")
if current_max > answer:
current_max = answer
continue
elif answer < secret:
print ("您的答案小了")
if current_min < answer:
current_min = answer
continue
else:
print("您的答案正确!")
print("共进行了{0}轮,现在开始新一轮游戏!".format(step))
print("------------------------")
ResetGame()
请问你有没有教材提供
解密作业
import random
secret = random.randint(1,10)
shuzi = input('请猜一个1-10的数字,你有三次机会,开始吧:')
zhengshu = int(shuzi)
times=2
while zhengshu !=secret and times>0:
if zhengshu > secret:
shuzi = input('大了,你还有'+str(times)+'次机会,再来一次:')
else:
if zhengshu < secret:
shuzi = input('小了,你还有'+str(times)+'次机会,再来一次:')
zhengshu = int(shuzi)
times = times-1
if zhengshu != secret:
print('三次机会用完咯,再见!')
else:
print('恭喜答对了,再见!')
课后练习 都看不了啊 还要鱼币
升级vip会员吧
我连注册也注册不了:c
請問課後練習的解答要在哪裡取得呢? 謝謝
這是我的解答,給大家參考
感覺仍可以再精簡,希望有人可以指正一下
設定可輸入5次,第二次開始會給提示
import random
i = random.randint (1,10)
num = input("猜猜一個1~10數字,有五次機會")
guess = int(num)
count = 5
if guess == i:
print("
答對了")
else:
if guess < i:
count = count-1
print("
還有"+str(count)+"次機會")
else:
count = count-1
print("
還有"+str(count)+"次機會")
while guess!= i and count != 1 :
num = input("
再來一次")
guess = int(num)
if guess == i:
print("
答對了")
else:
if guess < i:
count = count-1
print("
給你點提示,太小"+"
還有"+str(count)+"次機會")
else:
count = count-1
print("
給你點提示,太大"+"
還有"+str(count)+"次機會")
num = input("
最後機會了")
guess = int(num)
if guess == i:
print("
終於答對了")
else:
if guess < i:
count = count-1
print("
沒有機會了QQ")
print("
遊戲結束"+",答案是"+str(i))
else:
count = count-1
print("
沒有機會了QQ")
print("
遊戲結束"+",答案是"+str(i))
import random
ans = random.randint(1,100)
t=1
temp = input('input your number')
number = int(temp)
while number != ans and t3:
print ('no remaining chance,answer is %s'%ans)
else:
temp = input('input your number again')
number = int(temp)
if number == ans:
print ('good job')
print ('finish')
好像可以这样
#文字游戏改进版本
import random#导入random模块
secret = random.randint(1,10)#定义心里想的那个变量
print('-------------------我爱鱼c工作室-------------------')
temp = input('不妨猜一下小甲鱼心里想的是哪个数字:')#变量需要先定义再使用
guess = int(temp)#判断用户输入变量是否为整型
while guess != secret:#使用while循环条件
if guess > secret:
print('哥,大了大了-..-')
else:
print('嘿,小了!小了1')
temp = input('哎呀,猜错了,请重新输入把:')
guess = int(temp)
if guess == secret:
print('卧槽,你是小甲鱼心里的蛔虫吗?!')
print('哼,猜中了也没有奖励!')
print('游戏结束,不玩啦0.0')
import random
prompt = "
1:继续游戏(按任意键) 2:退出游戏(输入“quit”结束):"
while True:
user = int(input("请输入1--6之间的数字:"))
computer = random.randint(1,7)
if 1
小甲鱼工作室
你一共有四次机会
请输入数字:50
大了大了
猜错了,你还有3次机会
重新输入吧:25
小了小了
猜错了,你还有2次机会
重新输入吧:38
大了大了
猜错了,你还有1次机会
重新输入吧:30
恭喜答对了
游戏结束
import random
secret = random.randint(1,10)
times = 2
temp = input("请猜一个1-10的数字,你有三次机会,开始吧:")
guess = int(temp)
while guess != secret and times > 0:
if guess > secret:
temp = input("你猜大了,你还有" + str(times) + "次机会,再来一次:")
guess = int(temp)
else:
temp = input("你猜小了,你还有" + str(times) + "次机会,再来一次:")
guess = int(temp)
times = times -1
if guess == secret:
print("我草,你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没奖励!")
print("游戏结束,不玩了!")
else:
print("你好笨,猜了三次都不对!")
这个random模块存放在什么地方?
這是我的解答。
import random
ans = random.randint(1,10)
print("提示1~10之間")
temp = input("猜數字瞜(只有3次機會唷!):")
guess = int(temp)
frequency = 2 #次數
while ((guess != ans)and(frequency > 0)):
if guess == ans:
print('答對拉')
elif guess < ans:
frequency = frequency-1
print('太小啦!')
print('猜錯啦~你還有'+str(frequency)+'次機會')
guess = int(input('請重新輸入:'))
continue
elif guess > ans:
frequency = frequency - 1
print('太大了')
print('猜錯啦~你還有'+str(frequency)+'次機會')
guess = int(input('請重新輸入:'))
else:
print('次數用完拉~')
print('_______________')
print('~次數用完拉~')
print('正確數字是:'+str(ans))
print("遊戲結束")
实现了随机取数字并且三次机会,仅供参考~
import random #this is the first comments
se=random.randint(1,10)
times=3
n1=0
while (n1!=se) and (times!=0):
n=input('Again name a number')
while n.isdigit()==0:
n=input('Opps, you have to input a number, Please input a number')
n1=int(n)
if n1==se:
print('bingo')
else:
if n1>se:
print('too large, take a smaller one')
else:
print('too small, try a larger guess')
times=times-1
print('remaining chances to guess is ',times, 'times')
print('game over')
为什么把while循环里面的guess=int(temp)那一行去掉后就不能正常运行了,要么提示数字太大,要么只说太小
我的理解是,要调用变量就要 先给变量赋值,去掉那一段guess就没有赋值
因为temp = input 输入的是字符,secret选取随机整数,只有先把guess定义为只能输入整数才可以和secret提供的随机整数比较大小,所以是guess=int(temp)。因为在while里面如果删掉guess,语句只执行guess和secret的大小比较,guess没有定义比较是没有意义的
显示这个错误是什么意思啊,有谁知道吗?求大神解答下Traceback (most recent call last):
File "E:/python/新建文件夹/001.py", line 14, in
time=time+1
NameError: name 'time' is not defined
time是个变量,但是还没定义你就拿来用来了,先给time初始化赋个值,例如time=0
王浩 没赋值
"""這邊是我的課後解答,大家可參考看看"""
"""還沒寫完的別點開"""
import random
print ("遊戲開始")
secret = random.randint(1,100)
temp = input("請猜1~100之間的數字:")
guess = int(temp)
if guess == secret:
print ("恭喜猜中!答案是"+secret)
else:
print ("錯!")
if guess > secret:
print("太大惹!")
else:
print("太小惹!")
while guess != secret:
temp = input("請繼續猜:")
guess = int(temp)
if guess == secret:
print ("恭喜,終於猜中啦!答案是"+secret)
else:
print ("還是錯!")
if guess > secret:
print("還是太大惹")
else:
print("還是太小惹")
print("遊戲結束")
print ("恭喜,終於猜中啦!答案是"+secret) 會有錯誤語法顯示
需改成 print ("恭喜,終於猜中啦!答案是"+str(secret))
为何我这里在:print ("恭喜,終於猜中啦!答案是"+secret)的时候会提示TypeError: must be str, not int
哦哦,看到答案了。
试着瞎写了一个:
import random
secret = random.randint(1,10)
print('=====My first Python Work=====')
temp=input("Please input a number, you have 5 chances left:")
guess=int(temp)
t=1
if guess==secret:
print("Congratulations! You are right.")
else:
if guess>secret:
print("Sorry, it's too large! You have",5-t,"chances left:")
else:
print("Sorry, it's too small! You have",5-t,"chances left:")
while guess!=secret:
t=t+1
if (5-t)>=0:
temp=input("Plese input a number:")
guess=int(temp)
if guess==secret:
print("Congratulations! You are right.")
else:
if guess>secret:
print("Sorry, it's too large! You have",5-t,"chances left:")
else:
print("Sorry, it's too small! You have",5-t,"chances left:")
else:
print("No chances anymore!")
break
print("==========GAME OVER==========")
根据各位大神的改了一下,顺利运行了,就是还是不会限定次数。
import random
print("GAME STAR")
secret = random.randint(1,5)
temp = input("guess the number between 1-5: ")
guess = int(temp)
if guess == secret:
print("Amazing! The answer is " + str(secret))
else:
print("Wrong!")
if guess > secret:
print("More!")
else:
print("Less!")
while guess != secret:
temp = input("Please continu: ")
guess = int(temp)
if guess ==secret:
print("Right! That's " + str(secret)) #Final show: Right! That's 5
else:
print("Oh! NO! Wrong!")
if guess > secret:
print("More again!")
else:
print("Less again!")
print("GAME OVER! ")
你试试这个
print('猜数字游戏,你只有三次机会,加油哦')
import random
secret = random.randint(1,10)
temp = input('请输入一个整数:')
time = 3
guess = int(temp)
while(guess != secret and time >0):
if guess > secret:
print('猜错了,大了')
else:
print('猜错了,小了')
time = time - 1
temp = input('请重新输入:')
guess = int(temp)
print('小心,你的剩余机会只有:'+str(time))
if guess == secret:
print('回答正确了,游戏结束')
else:
print('3次机会用完你输了!!!正确答案是:'+str(secret))
import random
ans = random.randint(1, 10)
temp = input('guesss what I am thinking: ')
guess = int(temp)
n = 0
while (guess != ans) & (n < 2):
n = n + 1
if guess > ans:
guess = int(input('bigger, please try again: '))
else:
guess = int(input('smaller, please try again: '))
if guess == ans:
print('correct! Game is over now')
else:
print('already 3 times, GG, ans is:', str(ans))
elif Statements
The elif (short for else if) statement is a shortcut to use when chaining if and else statements.
A series of if elif statements can have a final else block, which is called if none of the if or elif expressions is True.
For example:
num = 7
if num == 5:
print("Number is 5")
elif num == 11:
print("Number is 11")
elif num == 7:
print("Number is 7")
else:
print("Number isn't 5, 11 or 7")
Result:
>>>
Number is 7
>>>
贴一个我的答案
import random
secret=random.randint(1,10)#生成1到10的整型随机数
print("-----------------Guess number game------------------")
temp=input("Please input a number,you have 3 chances in total:")
number=int(temp)
chance = 2
if number==secret:
print("awesome,you got the anwser 1st time")
print("congratulations")
else:
while chance>0 or number==secret:
if numbersecret:
print("too big, " + "you only have " + str(chance) + " chance now")
else :
print("congratulations")
break
chance = chance - 1
temp = input("Please input a new number:")
number = int(temp)
print("Game Over")
import random
passs = random.randint(1,10)
temp = int()
guess = int(temp)
a = 3
while guess != passs:
temp = input("inset")
guess = int(temp)
a = a - 1
if guess == passs:
print("yes good")
break
elif guess > passs:
print("bigger",a)
elif a
這是我的答案,沒頭緒的請參考~
應該還可以更精簡的樣子....歡迎討論,謝謝指教!
(第一次答錯也會給提示+只有三次機會+猜錯三次後會給你答案,額外小彩蛋:第一次就猜對心有靈C+猜到1-10以外的數字會7PUPU罵你喔!)
import random
secret = random.randint(1,10)
print("
"+"~"*20+"我的第一個小遊戲"+"~"*20+"
")
temp=input("請猜猜我現在心裡面想的數字(1-10):")
guess=int(temp)
if guess==secret:
print("
"+"~"*10+"good job!你猜對了!一次就猜中我們心有靈C~"+"~"*10+"
")
print(" █ █")
print(" ███ ███")
print(" █████████")
print(" █████████")
print(" ███████")
print(" █████")
print(" ███")
print(" █")
if guess>10 or guess
贴一个我的练习可以交流一下
import random
n = random.randint(1,10)
chance = 3
print ('guess a number from 1 to 10, you have just 3 times to guess')
antwort = int(input('enter your number:'))
while antwort != n and chance > 1:
if antwort > n:
print ('Falsch, zu gross')
else:
print ('Falsch, zu wenig')
chance = chance - 1
print ('your chance left',chance)
antwort = int(input('enter your number again:'))
if antwort == n:
print ("richtig")
else:
print ('your chance has been wasted. The right one is', n)
有一个问题,就是为什么chance 变成1了之后还是能进入whlie 循环?
@@kolyaswan7567 我也是这个问题,一直猜错的话怎么停止while循环,我的change都可以循环的负数了,你有找到答案了吗?记得分享一下。
@@nanzhang6717 我明白我的问题了,你的问题估计是你代码写错了...不然不可能到负数的
没有鱼币是不是就看不了作业啊😥
没有猜到。。。
作業 XDD
---------------------------
#呼叫隨機模組
import random
#設定變數[隨機數字] 為1~10的整數數字
隨機數字 = int(random.randint(1,10))
#顯示 '請輸入數字!' 並將使用者輸入的整數值回傳給 變數[輸入數字]
輸入數字 = int(input('請輸入數字!'))
#猜的次數 預設0次
次數 = int(0)
#顯示系統給的隨機數字(自己偷看用的 無意義 可省略)
print(隨機數字)
#當 [輸入數字] 不等於 [隨機數字] 和 輸入次數小於3次 有一個違反便跳出迴圈:
while 輸入數字 != 隨機數字 and 次數 隨機數字 :
print('太大啦啦')
elif 輸入數字 < 隨機數字 :
print('太小啦啦')
elif 輸入數字 == 隨機數字 :
print('猜對啦啦')
輸入數字 = int(input('再猜猜看!'))
#迴圈跑一次 [次數] 被+1
次數 = int(次數+1)
#顯示還有幾次機會 (數值類型要用str() 用int()會報錯 我也不知道為啥)
print('還有'+ str(3-次數)+'次機會')
#跳出迴圈後判斷到底有沒有答對
if 輸入數字 != 隨機數字 :
print('還是沒答對
結束遊戲')
elif 輸入數字 == 隨機數字 :
print('答對
結束遊戲')
计次循环首
good
我来晚了
import random
secret = random.randint(1, 10)
print(str(secret)+"
")
print("你有3次答題機會
")
""""猜第1次"""
temp = input("來猜個數字吧!
")
guess = int(temp)
"""剩下2次"""
times = 2
"""答案不正確且次數要大於0才會進入重複迴圈"""
while guess != secret and times > 0:
if guess > secret:
print("大了
")
print("你還有"+str(times)+"次機會")
temp = input("猜錯了~請重新輸入!
")
guess = int(temp)
times = times - 1
else:
print("小了
")
print("你還有" + str(times) + "次機會
")
temp = input("猜錯了~請重新輸入!
")
guess = int(temp)
times = times - 1
""""當答題次數用完或答對時"""
"""這裡判斷達錯了,還是答對了,因為有可能是因為次數沒了跳出,所以判斷是否猜對"""
if secret != guess:
print("你一直答錯,超出答題次數囉!")
else:
print("恭喜答對了!")
print("正確數字是"+str(secret))
print("遊戲結束囉!^_^")
用不了
小鱼的声音有够温柔
zhichi
secret = random.randint(1,20)
print("-----------------------game-----------------")
temp = input("guesses one number:")
guess = int(temp)
ti = 1
while (guess != secret) and (ti secret:
print("wow, too large")
print("you need change your number smaller!")
else:
print("wow, too small")
print("you need change your number larger!")
temp = input(" guesses another number:
")
guess = int(temp)
ti =ti + 1
print("chance left:" +str(3-ti))
if guess == secret:
print("fuck, you are right
you got nothing eventhought you are right!
game over~~~")
else:
print("ohou, no chance any moer")
print(secret)
小甲鱼,我想对你表白
好坑啊,就是骗钱的,答案根本看不了,购买什么金币才可以看,坑死了
good