鱼C论坛

 找回密码
 立即注册
查看: 3166|回复: 25

本来解决的一个输入类型转换问题。还需要求助一下

[复制链接]
发表于 2019-10-31 20:41:40 | 显示全部楼层 |阅读模式

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

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

x
实现一个带有变量x的函数random_converter(x)。 然后返回已经随机转换为int,float,bool,string或complex的x值。
例如,对于x = 12(整数),random_converter(x)可以返回“ 12”(字符串)或12.0(浮点数)。
进一步的说明:
x可以是int,float,bool,string或complex的任何类型。
分配必须是真正随机的,也就是说,如果重复几次,将导致不同的结果。
如果x不能转换(例如,字符串“ house”不能转换为数字),则打印“无法转换”,并且不返回任何值。

import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):
        value=r.choice(a)
        if value == 'int':
                x = int(x)
                if str.isnumeric(x) is False:
                        print("cannot be converted")
                        return None
                else:
                        print(x,type(x))
        if value == 'float':
                x = float(x)
                if str.isnumeric(x)  is False:
                        print("cannot be converted")
                        return None
                else:
                        print(x,type(x))
        if value == 'bool':
                x = bool(x)
                if str.isnumeric(x) is False:
                        print("cannot be converted")
                        return None
                else:
                        print(x,type(x))
        if value == 'str':
                x = str(x)
                if str.isnumeric(x) is False:
                        print("cannot be converted")
                        return None
                else:
                        print(x,type(x))
        if value == 'complex':
                x = complex(x)
                if str.isnumeric(x) is False:
                        print("cannot be converted")
                        return None
                else:
                        print(x,type(x))

这是我的代码,是存在错误的,因为本来可以用try来解决,但是老师说不能用,想一想其他的办法,但是这个办法我好像没写对,求助一下,谁帮我看看,我是哪里写错了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-10-31 21:16:08 | 显示全部楼层
有人吗?救命啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-31 21:27:23 | 显示全部楼层
  1. import random as r
  2. a = ['int', 'float', 'bool', 'str', 'complex']
  3. def random_converter(x):
  4.     value = r.choice(a)
  5.     try:
  6.         print(eval(value)(x))
  7.     except Exception:
  8.         print("无法转换")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-31 21:37:12 | 显示全部楼层

老大,我也想用这个,但是问题就是不能用try,怎么做?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-31 22:10:52 | 显示全部楼层

import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):
        value=r.choice(a)
        if x == str :
                print('cannot be converted')
                return None
        if value == 'int':
                x = int(x)
                print(x,type(x))
        if value == 'float':
                x = float(x)
                print(x,type(x))
        if value == 'bool':
                x = bool(x)
                print(x,type(x))
        if value == 'str':
                x = str(x)
                print(x,type(x))
        if value == 'complex':
                x = complex(x)
                print(x,type(x))
这样写对吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-31 22:25:04 | 显示全部楼层
本帖最后由 XiaoPaiShen 于 2019-10-31 22:32 编辑
qq819343713 发表于 2019-10-31 21:37
老大,我也想用这个,但是问题就是不能用try,怎么做?

  1. import random as r

  2. def check_number(num):
  3.     temp = str(num)
  4.     if temp.find('.') == -1:
  5.         prev, suff = temp, '0'
  6.     else:
  7.         prev, suff = temp.split('.', 1)
  8.         
  9.     if prev.isnumeric() and suff.isnumeric():
  10.         return True

  11.     return False

  12. def random_converter(x):
  13.     a = ['int','float','bool','str','complex']
  14.     convert_type = r.choice(a)

  15.     if convert_type == 'str':
  16.         result = str(x)
  17.         print(result,type(result))
  18.         return result
  19.    
  20.     elif not check_number(x):
  21.         print("cannot be converted")
  22.         return None

  23.     else:
  24.         result = eval(convert_type)(x)
  25.         print(result,type(result))
  26.         return result   

  27. def main():
  28.     for _ in range(10):
  29.         x = 123
  30.         random_converter(x)

  31. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-31 23:06:13 | 显示全部楼层

第一个功能是什么啊,大佬我看不懂了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-31 23:06:49 | 显示全部楼层

import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):
        value=r.choice(a)
        if type(x) == str :
                print('cannot be converted')
                return None
        else:
                if value == 'int':
                        x = int(x)
                        print(x,type(x))
                if value == 'float':
                        x = float(x)
                        print(x,type(x))
                if value == 'bool':
                        x = bool(x)
                        print(x,type(x))
                if value == 'str':
                        x = str(x)
                        print(x,type(x))
                if value == 'complex':
                        x = complex(x)
                        print(x,type(x))
我写成这样了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-1 02:30:42 | 显示全部楼层
qq819343713 发表于 2019-10-31 23:06
import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):

我们的输入可以是字符串,不能说字符型就不能转换,例如:‘12.5’
第一个方法是判断输入的字符串只是由数字和小数点组成的。
如果字符串中包含小数点,isnumeric()会返回False。
所以要把字符串从小数点处分开再判断
如果不包含小数点,就直接判断字符串是否全部由数字组成。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-1 03:24:28 | 显示全部楼层
XiaoPaiShen 发表于 2019-11-1 02:30
我们的输入可以是字符串,不能说字符型就不能转换,例如:‘12.5’
第一个方法是判断输入的字符串只是由 ...

谢谢,大佬,但是我也不能用isnumeric()。。。(很扯淡的要求。)
import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):
        value=r.choice(a)
        if value == 'int':
                if type(x) == complex:
                        print('connot be converted')
                        return None       
                else:
                        x = int(x)
                        print(x,type(x))
        if value == 'float':
                if type(x) == complex:
                        print('connot be converted')
                        return None       
                else:
                        x = int(x)
                        print(x,type(x))
                x = float(x)
                print(x,type(x))
        if value == 'bool':
                x = bool(x)
                print(x,type(x))       
        if value == 'str':
                x = str(x)
                print(x,type(x))
        if value == 'complex':
                x = complex(x)
                print(x,type(x))
我现在写成这样了。。。但还是问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-1 04:40:48 | 显示全部楼层
qq819343713 发表于 2019-11-1 03:24
谢谢,大佬,但是我也不能用isnumeric()。。。(很扯淡的要求。)
import random as r
a = ['int','flo ...

自己写check_number功能,find让用吗?

  1. import random as r

  2. def check_number(number):
  3.     str_number = '0123456789'
  4.     for char in number:        
  5.         if str_number.find(char) == -1:
  6.             return False

  7.     return True
  8.    
  9. def validate_input(source):
  10.     temp = str(source)
  11.     if temp.find('.') == -1:
  12.         prev, suff = temp, '0'
  13.     else:
  14.         prev, suff = temp.split('.', 1)   

  15.     if check_number(prev) and check_number(suff):
  16.         return True
  17.    
  18.     return False

  19. def random_converter(x):
  20.     a = ['int','float','bool','str','complex']
  21.     convert_type = r.choice(a)

  22.     if convert_type == 'str':
  23.         result = str(x)
  24.         print(result,type(result))
  25.         return result
  26.    
  27.     elif not validate_input(x):
  28.         print("cannot be converted")
  29.         return None

  30.     else:
  31.         result = eval(convert_type)(x)
  32.         print(result,type(result))
  33.         return result   

  34. def main():
  35.     for _ in range(10):
  36.         x = '123.5'
  37.         random_converter(x)

  38. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-1 04:50:15 | 显示全部楼层
XiaoPaiShen 发表于 2019-11-1 04:40
自己写check_number功能,find让用吗?

也不能啊,大哥,哎,所以我想了很久都不知道怎么办。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-1 04:52:08 From FishC Mobile | 显示全部楼层
老师让你们能用什么函数?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-1 04:56:45 From FishC Mobile | 显示全部楼层
for...in 字符串可以用吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-1 05:03:29 From FishC Mobile | 显示全部楼层
如果可以,就用 if...elif.... elif.....
逐个判断,等于“.”,0,1,2,...9
都要用引号引起来,字符串比较,任何一个相等就返回True,
如果都不符合就返回False
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-1 05:39:42 | 显示全部楼层
XiaoPaiShen 发表于 2019-11-1 05:03
如果可以,就用 if...elif.... elif.....
逐个判断,等于“.”,0,1,2,...9
都要用引号引起来,字符串比较 ...

import random as r
a = ['int','float','bool','str','complex']
def random_converter(x):
        value=r.choice(a)
        if value == 'int':
                if type(x) == complex:
                        print('connot be converted')
                        return None       
                else:
                        x = int(x)
                        print(x,type(x))
        elif value == 'float':
                if type(x) == complex:
                        print('connot be converted')
                        return None       
                else:
                        x = float(x)
                        print(x,type(x))       
        elif value == 'str':
                        x = str(x)
                        print(x,type(x))
        elif value == 'bool':
                x = bool(x)
                print(x,type(x))               
        elif value == 'complex':
                x = complex(x)
                print(x,type(x))
大佬,我写到这个地步了,但是比如  'aaaaa'转complex,和整数和浮点数就会报错而不是NONE。就这里不知道怎么解决,求教一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-1 05:40:31 | 显示全部楼层
XiaoPaiShen 发表于 2019-11-1 04:56
for...in 字符串可以用吗?

for in 是肯定可以的!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-1 06:05:52 | 显示全部楼层
本帖最后由 XiaoPaiShen 于 2019-11-1 06:09 编辑
qq819343713 发表于 2019-11-1 05:40
for in 是肯定可以的!!!


不能使用类型进行判断,因为输入的都是string, 尤其使用input键盘输入
  1. import random as r

  2. def validate_input(source):
  3.     temp = str(source)
  4.     dot_count = 0
  5.         
  6.     for char in temp:
  7.         if char == '.':
  8.             dot_count += 1
  9.         elif char == '0':
  10.             continue
  11.         elif char == '1':
  12.             continue
  13.         elif char == '2':
  14.             continue
  15.         elif char == '3':
  16.             continue
  17.         elif char == '4':
  18.             continue
  19.         elif char == '5':
  20.             continue
  21.         elif char == '6':
  22.             continue
  23.         elif char == '7':
  24.             continue
  25.         elif char == '8':
  26.             continue
  27.         elif char == '9':
  28.             continue
  29.         else:
  30.             return False

  31.     if dot_count <= 1:
  32.         return True        
  33.    
  34.     return False

  35. def random_converter(x):
  36.     a = ['int','float','bool','str','complex']
  37.     convert_type = r.choice(a)

  38.     if convert_type == 'str':
  39.         result = str(x)
  40.         print(result,type(result))
  41.         return result
  42.    
  43.     elif not validate_input(x):
  44.         print("cannot be converted")
  45.         return None

  46.     else:
  47.         x=str(x)
  48.         if convert_type == 'int':
  49.             # 先转成float,才能转成int, +0.5是为了四舍五入
  50.             result = int(float(x)+0.5)
  51.             print(result,type(result))
  52.             return result
  53.         elif convert_type == 'float':
  54.             result = float(x)
  55.             print(result,type(result))
  56.             return result
  57.         elif convert_type == 'bool':
  58.             result = bool(x)
  59.             print(result,type(result))
  60.             return result
  61.         elif convert_type == 'complex':
  62.             result = complex(x)
  63.             print(result,type(result))
  64.             return result
  65.         else:
  66.             print("cannot be converted to {0}".format(convert_type))
  67.             return None
  68.         

  69. def main():
  70.     for _ in range(10):
  71.         x = 123
  72.         random_converter(x)

  73. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-1 06:14:30 | 显示全部楼层
XiaoPaiShen 发表于 2019-11-1 06:05
不能使用类型进行判断,因为输入的都是string, 尤其使用input键盘输入

大哥,谢谢哈,但是我想问一下,前面一大串是啥东西。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-1 06:15:12 | 显示全部楼层
qq819343713 发表于 2019-11-1 06:14
大哥,谢谢哈,但是我想问一下,前面一大串是啥东西。。。

就是0.1.2.3那个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-20 11:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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