python输入一个值、返回另一个值,Python:如何分配2个值作为函数外部的值,我从1个输入的函数返回?...

作者分享了如何用Python构建一个函数is_ok检查输入是否为正整数,及其在get_value函数中的应用。遇到的问题包括如何处理函数返回的元组以及Python中正确使用input函数。建议改进的方法是分别检查变量的整数性和正负性。
摘要由CSDN通过智能技术生成

I'm starting out in python and thought it'd be a good exercise to try to build a function that determines whether an input passed to it is an integer, and if so whether it's positive. At a later stage I plan to reference that function from within a mathematical function that only accepts positive integers.

Anyway, this is the function I built:

def is_ok(x): #checks if x is a positive integer

is_int = False

is_pos = False

if type(x) == int:

is_int = True # if it's an integer, continue to test if >0

if x > 0:

is_pos = True

else:

pass

else:

pass

return is_int, is_pos

as you can see, it gets one argument (the value to be tested) and returns a tuple of Trues and Falses.

Now, I don't know how to use the tuple outside the function... for instance, if I try to pass a number through the function and assign variable the values received, I get an error. I mean something like:

is_int, is_pos = is_ok(y)

y being some number, for example. this produces an error on run.

So basically what I'm trying to understand is how to do the following:

function A accepts a value as its sole input ===> runs it through the is_ok(x) function ===> function A gets the tuple produced by is_ok() function and uses if/elifs to produce different results depending on the value of the tuple.

example for function A:

def get_value():

z = input("insert value" ")

is_ok(z)

if (is_int,is_pos) == (True, True):

print z**2

elif (is_int,is_pos) == (False, False):

print "Please enter an integer"

get_value()

elif (is_int, is_pos) == (True, False):

print "Please enter an positive integer"

get_value()

else:

print "something is wrong"

Help would be appreciated!

Thanks!

======================

EDIT: late addition

when I run this on input 7 for example (any positive integer for that matter):

def is_ok(x): #checks if x is a positive integer

is_int = False

is_pos = False

if type(x) == int:

is_int = True # if it's an integer, continue to test if >0

if x > 0:

is_pos = True

else:

pass

else:

pass

return is_int, is_pos

#is_int, is_pos = is_ok(y)

#print is_ok(y)

#print is_int

#print is_pos

#is_ok(7)

def get_value():

z = input("insert value ")

is_int, is_pos = is_ok(z)

if (is_int,is_pos) == (True, True):

print z**2

elif (is_int,is_pos) == (False, False):

print "Please enter an integer"

get_value()

elif (is_int, is_pos) == (True, False):

print "Please enter an positive integer"

get_value()

else:

print "something is wrong"

get_value()

I get:

49

(True, False)

First Q: why False?

Second Q: if false, why did it return what it should have for True, True

Third Q: why did it print the tuple? No print statement

More help? :)

解决方案

As chepner mentioned in the comments, you're on the right track but you're throwing away your results!

Personally, I wouldn't do what you're doing, I would check it separately for each thing (as opposed to using one function to check two facts about a variable and return each fact). But if you WANT to do it your way, here's what you'd need to do:

def is_ok(x): #checks if x is a positive integer

is_int = False

is_pos = False

if type(x) == int:

is_int = True # if it's an integer, continue to test if >0

if x > 0:

is_pos = True

else:

pass

else:

pass

return is_int, is_pos

def get_value():

z = input("insert value: ")

is_int, is_pos = is_ok(z)

if (is_int,is_pos) == (True, True): # could also do `if all(is_int,is_pos):`

print z**2

elif (is_int,is_pos) == (False, False):

print "Please enter an integer"

get_value()

elif (is_int, is_pos) == (True, False):

print "Please enter an positive integer"

get_value()

else:

print "something is wrong"

If I were you, I'd write something like:

def get_value():

while True:

z = input("Insert value: ") # DON'T DO THIS, INPUT() IS BADDDDD IN PYTHON2

# instead do:

## try: z = int(raw_input("Insert value: "))

## except ValueError:

## print("Please provide an integer")

## continue

# this also FORCES z to be an integer, so isinstance(z,int) is ALWAYS TRUE

is_int = isinstance(z,int)

is_pos = is_int and z>0 # this will keep you from throwing an exception if

# you can't compare z>0 because z isn't int

if is_int and is_pos:

print z**2

elif is_int:

print "Please provide an integer"

continue

else:

print "Integer must be positive"

continue

# nothing can ever get to that last else block you had.

The reason we don't use input in Python2 is that it executes whatever you write out as Python code. If I entered import os; for file in os.listdir("C:/windows/system32"): os.remove(file) it would delete every file in C:\Windows\System32. I don't think I have to tell you why that's bad! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值