Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python函数跳转_Python_Function - Fatal编程技术网

python函数跳转

python函数跳转,python,function,Python,Function,我的目的是从文件中读取一个列表,从该列表中选择一个随机数并将其删除,然后重新保存该列表。有时该函数工作正常,有时不正常 choices=[] with open("C:\\choices.txt", "r") as f: for line in f: choices.append(int(line.strip())) if len(choices)==0: choices=[0,1,2,3,4,

我的目的是从文件中读取一个列表,从该列表中选择一个随机数并将其删除,然后重新保存该列表。有时该函数工作正常,有时不正常

choices=[]

with open("C:\\choices.txt", "r") as f:
    for line in f: 
        choices.append(int(line.strip()))
        
if len(choices)==0:
        choices=[0,1,2,3,4,5]

num=rdm.randint(0,5)

def chooseline(myList, myNum):
    print("before if num= "+str(myNum))
    
    if not myNum in myList:
        myNum=rdm.randint(0,5)
        chooseline(myList,myNum)
        print("In NOT-IF num= "+str(myNum))#<-- Need to move before chooseline to print. ok
    else:
        myNum=myList.pop(myList.index(myNum))      
        print("in Else num= "+str(myNum))
    
    print("end num= "+str(myNum))
    return myNum


newnum= chooseline(choices,num)

with open("C:\\choices.txt", "w") as f:
    for e in choices:
        f.write(str(e) +"\n")
choices=[]
打开(“C:\\choices.txt”,“r”)作为f:
对于f中的行:
choices.append(int(line.strip()))
如果len(选项)==0:
选项=[0,1,2,3,4,5]
num=rdm.randint(0,5)
def选择器行(myList,myNum):
打印(“在if num=“+str(myNum)”之前)
如果myList中没有myNum:
myNum=rdm.randint(0,5)
选择行(myList,myNum)

print(“In NOT-IF num=“+str(myNum))#有一种更简单、更有效的方法

import random

lines = []
with open('choices.txt', 'r') as file:
    for line in file:
        lines.append(int(line.rstrip()))
    file.close()

with open('choices.txt', 'w') as file:
    if len(lines) > 1:
        number = lines[random.randint(0, len(lines) - 1)]
        for line in lines:
            if line != number:
                file.write(f'{line}\n')
    file.close()

没有注释中提到的递归的示例:

import random

choices = []

with open("choices.txt", "r") as f:
    for line in f:
        choices.append(int(line.strip()))

if len(choices) == 0:
    choices = [0, 1, 2, 3, 4, 5]

num = random.randint(0, 5)


def choose_num_in_list(max_attempts=100):
    chosen_number = -1
    attempts = 0
    while chosen_number == -1 and attempts < max_attempts:
        candiate_number = random.randint(0, 5)
        attempts = attempts + 1
        if candiate_number in choices:
            chosen_number = candiate_number
    return chosen_number


newnum = choose_num_in_list()
if(newnum != -1):
      choices.pop(choices.index(newnum))
      print(f"Removed {str(newnum)} from the list")
else:
    print("Failed to remove a number from the list")

with open("choices.txt", "w") as f:
    for e in choices:
        f.write(str(e) + "\n")
随机导入
选项=[]
以open(“choices.txt”、“r”)作为f:
对于f中的行:
choices.append(int(line.strip()))
如果len(选项)==0:
选项=[0,1,2,3,4,5]
num=random.randint(0,5)
def在_列表中选择_num_(最大尝试次数=100):
所选的_数=-1
尝试次数=0
选择时,次数==-1且尝试次数<最大尝试次数:
candiate_number=random.randint(0,5)
尝试次数=尝试次数+1
如果选择中有candiate_编号:
所选号码=糖果号码
返回所选号码
newnum=在\u列表()中选择\u num\u
如果(newnum!=-1):
choices.pop(choices.index(newnum))
打印(f“从列表中删除{str(newnum)}”)
其他:
打印(“未能从列表中删除数字”)
以open(“choices.txt”、“w”)作为f:
对于e in选项:
f、 写入(str(e)+“\n”)

您正在递归调用
chooseline()
,当“内部”调用完成时,“外部”调用将在停止的地方继续运行。使用循环而不是递归会更简单。另外,NOT-IF print语句显示新选择的myNum值,这可能与触发if的值不同。@John Gordon显然,你已经说过了。啊@约翰·戈登显然是你说的。啊!我知道Not if在打印它的声明之前显示了新选择的数字,现在我知道为什么了-Bamar是的,更简单,特别是对于仅6个元素。
import random

choices = []

with open("choices.txt", "r") as f:
    for line in f:
        choices.append(int(line.strip()))

if len(choices) == 0:
    choices = [0, 1, 2, 3, 4, 5]

num = random.randint(0, 5)


def choose_num_in_list(max_attempts=100):
    chosen_number = -1
    attempts = 0
    while chosen_number == -1 and attempts < max_attempts:
        candiate_number = random.randint(0, 5)
        attempts = attempts + 1
        if candiate_number in choices:
            chosen_number = candiate_number
    return chosen_number


newnum = choose_num_in_list()
if(newnum != -1):
      choices.pop(choices.index(newnum))
      print(f"Removed {str(newnum)} from the list")
else:
    print("Failed to remove a number from the list")

with open("choices.txt", "w") as f:
    for e in choices:
        f.write(str(e) + "\n")