鱼C论坛

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

[已解决]课后作业5

[复制链接]
发表于 2024-8-5 17:05:42 | 显示全部楼层 |阅读模式

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

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

x
课后作业5,这个为啥一直挑不出循环,按理说,只要输入int,就跳出来了呀。[code]temp = input('please input number:')

while type(temp) !=type(1):
     print("sorry,not int temp",end='')
     temp = input("please input a int'temp number:")
最佳答案
2024-8-5 18:11:09
代码模式后面少了一个[/code]

建议修改:

  1. temp =int( input('please input number:'))

  2. while type(temp) !=type(1):
  3.      print("sorry,not int temp",end='')
  4.      temp = input("please input a int'temp number:")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-8-5 18:00:31 | 显示全部楼层
知道了,因为input一直是str,刚着急了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-5 18:11:09 | 显示全部楼层    本楼为最佳答案   
代码模式后面少了一个[/code]

建议修改:

  1. temp =int( input('please input number:'))

  2. while type(temp) !=type(1):
  3.      print("sorry,not int temp",end='')
  4.      temp = input("please input a int'temp number:")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-5 18:54:57 | 显示全部楼层
你的代码无法跳出循环的原因是 input() 函数的返回值始终是字符串类型,即使用户输入了一个整数。因此,你需要将用户输入的字符串转换为整数。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-5 21:05:24 | 显示全部楼层
本帖最后由 jackz007 于 2024-8-5 21:09 编辑
  1. while True:
  2.     temp = input('input an integer : ')
  3.     try:
  4.         x = int(temp)
  5.         break
  6.     except:
  7.         print('wrong data type !\n')
  8. print(x)
复制代码

        运行实况:
  1. D:\[exercise]\Python>python x.py
  2. input an integer : 3.14
  3. wrong data type !

  4. input an integer : 314
  5. 314

  6. D:\[exercise]\Python>python x.py
  7. input an integer : abc
  8. wrong data type !

  9. input an integer : -2024
  10. -2024

  11. D:\[exercise]\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-8-6 09:08:15 | 显示全部楼层


input 函数返回值类型是 str 字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-20 02:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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