鱼C论坛

 找回密码
 立即注册
查看: 1169|回复: 5

请问这道题应该怎么添加才能实现呢

[复制链接]
发表于 2019-12-30 22:03:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
要求:当用户输入错误类型的时候,及时提醒用户重新输入,防止程序崩溃。小甲鱼提供的答案我还是不是很懂,希望大神指点迷津
import random
answer = random.randint (1,10)
guess = 0
times = 3
while (guess != answer)and(times > 0):
    temp = input('猜猜我心里想得是哪个数字:')
    guess = int(temp)
    if guess == answer:
        print('好棒,猜中了呀!')
    else:
        if guess > answer:  
            print('大了一点哦!')
        else:
            print('小了一点点哎~')
        times = times-1
while times == 0:
    print('好可惜机会用完了呀~~~~~~')
    break
print('游戏结束')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-30 22:11:11 | 显示全部楼层

回帖奖励 +1 鱼币

本帖最后由 jackz007 于 2019-12-30 22:12 编辑
  1. import random

  2. answer = random . randint (1 , 10)
  3. times = 3
  4. while times > 0:
  5.     if times < 3:
  6.         if guess > answer:  
  7.             print('大了一点哦!')
  8.         else:
  9.             print('小了一点点哎~')
  10.     guess = int(input('猜猜我心里想得是哪个数字:'))
  11.     times -= 1
  12.     if guess == answer:
  13.         print('好棒,猜中了呀!')
  14.         break
  15. else:
  16.     print('好可惜机会用完了呀~~~~~~')
  17. print('游戏结束')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-30 22:27:22 | 显示全部楼层

回帖奖励 +1 鱼币

本帖最后由 ba21 于 2019-12-30 23:16 编辑
  1. import random

  2. def GetInt(temp):
  3.     while not temp.isdigit():
  4.         temp = input('错误, 请输入数字:')
  5.     return int(temp)
  6.    
  7. answer = random.randint (1,10)
  8. guess = 0
  9. times = 3
  10. while (guess != answer)and(times > 0):
  11.     temp = input('猜猜我心里想得是哪个数字:')
  12.     guess = GetInt(temp)
  13.     if guess == answer:
  14.         print('好棒,猜中了呀!')
  15.     else:
  16.         if guess > answer:  
  17.             print('大了一点哦!')
  18.         else:
  19.             print('小了一点点哎~')
  20.         times = times-1
  21. while times == 0:
  22.     print('好可惜机会用完了呀~~~~~~')
  23.     break
  24. print('游戏结束')

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-30 22:28:53 | 显示全部楼层

回帖奖励 +1 鱼币

import random
answer = random.randint (1,10) #生成一个1至10之间的随机整数
guess = 0  #赋一个初始值0
times = 3 #赋一个初始值为3,这 里主要是表示有3次机会
while (guess != answer)and(times > 0): #如果guess和answer不相等,并且times>0 一直循环
   如果你想相实现你要的效果,就在这里再写一个循环,只要输入的不能转换为数字,就提示重新输入。不知道我理解对你的意思没有

    temp = input('猜猜我心里想得是哪个数字:') #手动输一个数
    guess = int(temp) #把这个数赋值给guess
    if guess == answer:
        print('好棒,猜中了呀!')
    else:
        if guess > answer:  
            print('大了一点哦!')
        else:
            print('小了一点点哎~')
        times = times-1
while times == 0:
    print('好可惜机会用完了呀~~~~~~')
    break
print('游戏结束')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-31 00:36:55 | 显示全部楼层
  1. import random
  2. answer = random.randint (1,10)
  3. guess = 0
  4. times = 3
  5. while (guess != answer)and(times > 0):
  6.     try:
  7.         temp = input('猜猜我心里想得是哪个数字:')
  8.         guess = int(temp)
  9.         if guess == answer:
  10.             print('好棒,猜中了呀!')
  11.         else:
  12.             if guess > answer:  
  13.                 print('大了一点哦!')
  14.             else:
  15.                 print('小了一点点哎~')
  16.             times = times-1
  17.     except:
  18.         print("输入错误")
  19. while times == 0:
  20.     print('好可惜机会用完了呀~~~~~~')
  21.     break
  22. print('游戏结束')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-31 08:49:38 | 显示全部楼层
  1. import random
  2. x = random.randint(1,10)

  3. zs = ("+")

  4. print(zs*15+"Hello World"+zs*15)

  5. cs = 3

  6. print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
  7. while cs > 0:
  8.       temp = input()
  9.       while not temp.isdigit():
  10.             temp = input("输入不合法,请输入正确的数字!请重新输入一个数字:")
  11.       guess = int(temp)
  12.       cs -= 1
  13.       if guess == x:
  14.             print("哎哟,不错啦,居然猜对了")
  15.             print("猜中也没用,哼哼!")
  16.       else:
  17.             if guess > x:
  18.                   print("哥~~大了哟")
  19.             else:
  20.                   print("嘿嘿嘿,小了哟")
  21.             if cs > 0:
  22.                   print("再试一次吧:", end=" ")
  23.             else:
  24.                   print("机会用光啦~~")

  25. print("游戏结束,不玩啦^_^")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 16:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表