小编Sha*_*mar的帖子

如果在使用makedirs创建文件夹时已经存在,如何覆盖该文件夹?

以下代码允许我创建一个目录(如果它尚不存在).

dir = 'path_to_my_folder'
if not os.path.exists(dir):
    os.makedirs(dir)
Run Code Online (Sandbox Code Playgroud)

程序将使用该文件夹将文本文件写入该文件夹.但是我想在下一次打开程序时从一个全新的空文件夹开始.

有没有办法覆盖文件夹(并创建一个具有相同名称的新文件夹),如果它已经存在?

python directory overwrite text-files

35
推荐指数
3
解决办法
5万
查看次数

如何打印阵列?

int main() {  
  int my array[3][3] =
    10, 23, 42,    
    1, 654, 0,  
    40652, 22, 0  
  };  

  printf("%d\n", my_array[3][3]);  
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我无法打印阵列..任何想法为什么?我是一名初学者,所以任何建议的话都会受到赞赏.

c printing arrays multidimensional-array

34
推荐指数
2
解决办法
43万
查看次数

Python查找列表中最大值的索引

def main():
    a = [2,1,5,234,3,44,7,6,4,5,9,11,12,14,13]
    max = 0
    for number in a:
        if number > max:
            max = number
    print max

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

我能够获得数组中的最大值(当然不使用max() ...).如何获得该值的索引(位置)?请尽量保持简单,不使用新的Python关键字或内置函数.谢谢!

python indexing list max

15
推荐指数
5
解决办法
5万
查看次数

Python替代全局变量

import random  

#----------------------------------------------#
def main():  
    create_list_and_find_max_and_min(10)  
    the_smart_way()
#----------------------------------------------#
def create_list_and_find_max_and_min(n):
    global my_array
    my_array = []
    n = input("How many numbers do you want in your array?:")
    for i in range(n):
        my_array.append(random.randint(1,n))
    print "My array is:", my_array
    #----------------------------------------------#
    min = my_array[0]
    for number in my_array:
    if min > number:
        min = number
    print "The minimum value in the array is:", min
    #----------------------------------------------#
    max = my_array[0]
    for number in my_array:
        if max < number:
        max = number
    print "The maximum value in …
Run Code Online (Sandbox Code Playgroud)

python function global-variables

6
推荐指数
1
解决办法
1万
查看次数

使用 Python 自动调整 Excel 工作表的所有列

有没有人知道任何 Python --> Excel 库,其中包含一种在给定 Excel 文件名的情况下自动调整工作表中所有列的方法?

python excel

6
推荐指数
1
解决办法
1万
查看次数

Python线程.Timer只重复一次

def On_Instrumentation_StartAnimation():  

    """
    Syntax      : On_Instrumentation_StartAnimation()
    Purpose     : Fired if the animation is started
    Parameters  : None
    """
    print "----------------------------------------------------------------------------------------"
    localtime = time.asctime(time.localtime(time.time()))
    global start
    start = time.clock()
    print "The user entered Animation Mode at local time: ", localtime
    print("This data has also been written to 'c:\dSPACE71\cdlog\cdlog.txt'")   
    threading.Timer(2, ExecuteDemo).start()
    ExecuteDemo()
    # Printing to the text file
    file1 = open('c:\dSPACE71\cdlog\cdlog.txt', 'a')
    file1.write("\n----------------------------------------------------------------------------------------")
    file1.write("\nThe user entered Animation Mode at local time: ")
    file1.write(localtime)
    file1.close()      

def ExecuteDemo()
    .
    .
    .
    Current_value = Current.Read() …
Run Code Online (Sandbox Code Playgroud)

python multithreading

4
推荐指数
1
解决办法
1万
查看次数

Python从各种函数返回变量

from __future__ import division
import math

def main():
    the_discriminant = discrim(1,0,-4)
    print the_discriminant
    the_rest(discrim,b,a)

def discrim(a,b,c):
    discriminant = math.sqrt(math.pow(b,2)-4*a*c)
    return discriminant, b,a

def the_rest(discrim,b,a):
    x = ((-b + discriminant) / 2*a)
    y = ((-b - discriminant) / 2*a)
    print x,y

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

我是Python的新手,我正在编写函数和返回变量,我对如何纠正代码有点困惑.我正在编写一个二次求解器程序,但我需要在"其余"函数中使用判别式和a,b,c值.(这完成了其余的等式.)我对如何返回值并在另一个函数中使用它感到困惑.谢谢!

python variables return function

2
推荐指数
1
解决办法
296
查看次数

Python字符串格式问题

import random

def main():
    the_number = random.randint(1,100)
    guess = 0
    no_of_tries = 0
    while guess != the_number:
        no_of_tries += 1
        guess = int(input("Enter your guess: "))
        if guess < the_number:
            print "--------------------------------------"
            print "Guess higher!", "You guessed:", guess
            if guess == the_number - 1:
                print "You're so close!"
        if guess > the_number:
            print "--------------------------------------"
            print "Guess lower!", "You guessed:", guess
            if guess == the_number + 1:
                print "You're so close!"
        if guess == the_number:
            print "--------------------------------------"
            print "You guessed correctly! …
Run Code Online (Sandbox Code Playgroud)

python string

2
推荐指数
1
解决办法
76
查看次数

将相同的参数传递给多个函数 - Python

在下面的GPA计算程序中,我的两个函数采用相同的参数.什么是相同的参数传递给两个或多个功能的最佳方式?谢谢!

def main():
    cumulative(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4)
    print "Your semester 5 gpa is:", sem_5(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4)[0]

def cumulative(ee311, cpre281, math207, ee332, jlmc101):
    qpts_so_far = 52 + 40.97 + 47.71 + 49
    total_qpts = qpts_so_far + sem_5(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4)[1]
    total_gpa = total_qpts/(13 + 13 + 13 + 15 + 17)
    print "Your cumulative GPA is:", total_gpa

def sem_5(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4):
    sem_5_qpts = 4*ee311 + 4*cpre281 + 3*math207 + 3*ee332 + 3*jlmc101
    sem_5_gpa = (sem_5_qpts)/17.0 …
Run Code Online (Sandbox Code Playgroud)

python arguments function parameter-passing

2
推荐指数
1
解决办法
3104
查看次数

为什么Python打印括号?

当我运行下面的代码时,会弹出以下消息框.为什么要打印那些我不想要它们的烦人牙箍呢?我使用的是Python 2.7和EasyGui.

在此输入图像描述

from __future__ import division
from easygui import *
import ystockquote

def main():
    PRHSX_message()

def PRHSX_message():
    prhsx_share_price = float(ystockquote.get_last_trade_price('PRHSX'))
    prhsx_daily_change = ystockquote.get_change('PRHSX')
    prhsx_total_shares = 47.527
    prhsx_starting_value = 2500
    prhsx_percent_change = (((prhsx_share_price * prhsx_total_shares) / prhsx_starting_value) - 1) * 100
    prhsx_percent_change = str("%.2f" % prhsx_percent_change) + "%"
    prhsx_current_value = prhsx_share_price * prhsx_total_shares
    prhsx_profit = prhsx_current_value - prhsx_starting_value
    prhsx_current_value =  "$" + str("%.2f" % prhsx_current_value)
    prhsx_profit = "$" + str("%.2f" % prhsx_profit)
    prhsx_string = "T. Rowe Price Roth IRA Account\n____________________________\n   \ …
Run Code Online (Sandbox Code Playgroud)

python string formatting easygui

2
推荐指数
1
解决办法
250
查看次数