Python基础-第一个项目/挑战

Python基础-第一个项目/挑战,python,Python,我对Python(以及一般的软件编程/开发)非常陌生。我决定使用下面的场景作为我的第一个项目。该项目包括5个主要的个人挑战。有些挑战我已经能够完成(虽然可能不是最有效的方式),还有一些我正在努力解决。非常感谢您对我的方法和改进建议的任何反馈 项目场景=“如果我在100天内每天将我的钱翻一番,我在第100天会得到多少?我在第1天的起始金额是1.00美元” 1.)挑战1-第100天之后的净总数是多少?(我想已完成,如果我错了,请纠正我) 2.)挑战2-打印以筛选第一列中的天数,并在第二列中筛选相应的

我对Python(以及一般的软件编程/开发)非常陌生。我决定使用下面的场景作为我的第一个项目。该项目包括5个主要的个人挑战。有些挑战我已经能够完成(虽然可能不是最有效的方式),还有一些我正在努力解决。非常感谢您对我的方法和改进建议的任何反馈

项目场景=“如果我在100天内每天将我的钱翻一番,我在第100天会得到多少?我在第1天的起始金额是1.00美元”

1.)挑战1-第100天之后的净总数是多少?(我想已完成,如果我错了,请纠正我)

2.)挑战2-打印以筛选第一列中的天数,并在第二列中筛选相应的每日总数。-(我想已完成,如果我错了,请纠正我)

3.)挑战3-将总结果(100天后)写入外部txt文件-(我认为已完成,如果我错了,请纠正我)

4.)挑战4-将计算出的每日运行总数写入外部txt文件。第1列为当天,第2列为金额。因此,就像挑战2一样,只需要一个外部文件而不是屏幕

NEED HELP, I can't seem to figure this one out.
5.)挑战5-以某种方式绘制或绘制每日结果(基于#4)
需要指导。

在我开始我的个人Python之旅时,我感谢大家的反馈

  • 您可以使用在挑战3中所做的操作来打开和关闭输出文件
  • 在这两者之间,你必须像在挑战2中那样计算每天的数据。
    • 您将不得不将每日结果合并成字符串,而不是将其写入流中。之后,您可以将该字符串写入文件,就像在挑战3中一样

挑战2

这可以正常工作,但不需要编写
列表(范围(101))
,您只需编写
范围(101)
。事实上,甚至不需要创建一个变量来存储它,您可以这样做:

for x in range(101):
    print("whatever you want to go here")
挑战3

同样,这也可以很好地工作,但是在写入文件时,通常最好使用
with
语句,这意味着您不需要在最后关闭文件,因为python会处理这个问题。例如:

with open("calctest.txt", "w") as f:
    write(str(hundred_days))
挑战4

使用for循环,就像在挑战2中一样。使用“\n”写新行。同样,在with语句中执行所有操作。e、 g

with open("calctest.txt", "w") as f:
    for x in range(101):
        f.write("something here \n").
(会写一个文件,其中“某物在这里”写了101次)

挑战5

有一个名为matplotlib的python库,我从未使用过,但我建议,为了解决此任务,可以使用该库


我希望这能有所帮助:)

挑战一:

这是正确的方法

days = 100
compound_rate = 2
print("Result after 100 days" + (compound_rate ** days))
挑战二

这是正确的

compound_rate = 2                        
days_range = list(range(101))               
for x in days_range:
    print(x + (compound_rate ** x))
挑战三

这一个很接近,但是您不需要将百天的结果转换为字符串,因为您可以将整数写入文件,而python在大多数情况下并不关心。当以某种方式使用数据而不是简单地打印数据时,只需担心显式强制转换

compound_rate = 2                           
days_range = list(range(101))
hundred_days = (compound_rate ** 100)

textFile = open("calctest.txt", "w")
textFile.write(hundred_days)
textFile.close()
挑战四

对于这个挑战,您将需要研究python CSV模块。使用此模块,您可以非常简单地将数据写入两行,以逗号分隔

挑战五


对于这一挑战,您将需要研究python库matplotlib。此库将为您提供以图形方式处理数据的工具。

挑战1的答案如下:

l=[]

对于范围(0100)内的a:


打印(“100天后总计”,总和(l))

非常感谢您抽出时间回复。这是非常有用的信息。我仍在试图找出进入f.write(“此处的某物”)的实际计算,但将继续尝试完成它。我试着在挑战2中使用同样的计算方法,但似乎有些不对劲。不用担心。如果您觉得它很有用,请接受答案或投赞成票:)我想问题是print()做了一些write()做不到的事情。首先,print()接受许多参数,因此您可以只列出需要打印的内容,它还会自动将整数转换为字符串。write()只接受一个必须是单个字符串的参数。因此,您需要对代码进行适当的调整。要组合字符串,只需使用“+”,要将整数转换为字符串,请使用str()。例如,在挑战2中,
file.write(str(x)+str(component_rate**int(x))
注意,
str(x)
中的str()实际上是不必要的,因为print()不必接受字符串。而且,
x
应该已经是一个整数,所以实际上不需要
int(x)
<代码>str(复合费率**x)应该可以。谢谢您的回复,彼得!我真的很感激。我仍在努力解决细节问题,但我知道这是整个过程的一部分。我能够通过以下几点来实现它用open(“calctest.txt”,“w”)作为f:for范围(101)内的x:#f.write(“此处的某物\n”)。f、 写('Day#'+str(x)+'='+'$'+(str(float(复合汇率**int(x))))+'\n')”
days = 100
compound_rate = 2
print("Result after 100 days" + (compound_rate ** days))
compound_rate = 2                        
days_range = list(range(101))               
for x in days_range:
    print(x + (compound_rate ** x))
compound_rate = 2                           
days_range = list(range(101))
hundred_days = (compound_rate ** 100)

textFile = open("calctest.txt", "w")
textFile.write(hundred_days)
textFile.close()
    b = 2 ** a

    l.append(b)
import os, sys
import datetime
import time

#to get the current work directory, we use below os.getcwd()
print(os.getcwd())

#to get the list of files and folders in a path, we use os.listdir
print(os.listdir())

#to know the files inside a folder using path 
spath = (r'C:\Users\7char')
l = spath
print(os.listdir(l))

#converting a file format to other, ex: txt to py
path = r'C:\Users\7char'
print(os.listdir(path))
# after looking at the list of files, we choose to change 'rough.py' 'rough.txt'
os.chdir(path)
os.rename('rough.py','rough.txt')
#check whether the file has changed to new format
print(os.listdir(path))
#yes now the file is changed to new format


print(os.stat('rough.txt').st_size)
# by using os.stat function we can see the size of file (os.stat(file).sst_size)

path = r"C:\Users\7char\rough.txt"
datetime = os.path.getmtime(path)
moddatetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(datetime))
print("Last Modified Time : ", moddatetime)

#differentiating b/w files and folders using - os.path.splitext
import os
path = r"C:\Users\7char\rough.txt"
dir(os.path)
files = os.listdir()
for file in files:
    print(os.path.splitext(file))

#moving a file from one folder to other (including moving with folders of a path or moving into subforlders)
import os
char_7 = r"C:\Users\7char"
cleardata = r"C:\Users\clearadata" 
operating = os.listdir(r"C:\Users\7char")
print(operating)
for i in operating:
    movefrom = os.path.join(char_7,i)
    moveto = os.path.join(cleardata,i) 
    print(movefrom,moveto)
    os.rename(movefrom,moveto)

#now moving files based on length of individual charecter (even / odd) to a specified path (even or odd).
import os

origin_path = r"C:\Users\movefilehere"
fivechar_path= r"C:\Users\5char"
sevenchar_path = r"C:\Users\7char"   

origin_path = os.listdir(origin_path)
    
for file_name in origin_pathlist:
    l = len(file_name)
    if l % 2 == 0:   
        evenfilepath = os.path.join(origin_path,file_name)
        newevenfilepath = os.path.join(fivechar_path,file_name)
        print(evenfilepath,newevenfilepath)
        os.rename(evenfilepath,newevenfilepath)        
    else:
        oddfilepath = os.path.join(origin_path,file_name)
        newoddfilepath = os.path.join(sevenchar_path,file_name)
        print(oddfilepath,newoddfilepath)
        os.rename(oddfilepath,newoddfilepath)
    

#finding the extension in a folder using isdir
import os

path = r"C:\Users\7char"
print(os.path.isdir(path))            

#how a many files .py and .txt (any files) in a folder
import os
from os.path import join, splitext
from glob import glob
from collections import Counter

path = r"C:\Users\7char"

c = Counter([splitext(i)[1][1:] for i in glob(join(path, '*'))])
for ext, count in c.most_common():
    print(ext, count)

#looking at the files and extensions, including the total of extensions.
import os
from os.path import join, splitext
from collections import defaultdict

path = r"C:\Users\7char"
c = defaultdict(int)
files = os.listdir(path)
for filenames in files:
    extension = os.path.splitext(filenames)[-1]
    c[extension]+=1
    print(os.path.splitext(filenames))
print(c,extension)

#getting list from range
list(range(4))

#break and continue statements and else clauses on loops
for n in range(2,10):
    for x in range(2,n):
        if n%x == 0:
            print(n,'equals',x, '*', n//x)
            break
    else:
        print(n, 'is a prime number')

#Dictionaries
#the dict() constructer builds dictionaries directly from sequences of key-value pairs
dict([('ad', 1212),('dasd', 2323),('grsfd',43324)])

#loop over two or more sequences at the same time, the entries can be paired with the zip() function.
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
    print('What is your {0}?  It is {1}.'.format(q, a))

#Using set()
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
for f in sorted(set(basket)):
    print(f)