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

时间:2017-12-03 16:45:12

标签: python

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

项目情景 ="如果我每天将我的钱翻倍100天,我会在第100天结束多少钱?我在第1天的起始金额是$ 1.00"

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

days = 100
compound_rate = 2
print('compound_rate ** days) # 2 raised to the 100th

#==Result===
1267650600228229401496703205376

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

    compound_rate = 2                        
    days_range = list(range(101))               
    for x in days_range:
        print (str(x),(compound_rate ** int(x)))

# ===EXAMPLE Results
# 0 1
# 1 2
# 2 4
# 3 8
# 4 16
# 5 32
# 6 64
# 100 1267650600228229401496703205376

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

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

textFile = open("calctest.txt", "w")
textFile.write(str(hundred_days))
textFile.close()
#===Result====
string of 1267650600228229401496703205376 --> written to my file 'calctest.txt'

4.。挑战4 - 将计算的正在运行的DAILY Totals写入外部txt文件。第1列将是Day,第2列将是Amount。就像挑战#2一样,但是外部文件而不是屏幕

NEED HELP, I can't seem to figure this one out.

5。)挑战5 - 以某种方式绘制或绘制每日结果(基于#4) - NEED GUIDANCE.

在我开始个人Python之旅时,我感谢所有人的反馈!

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

挑战2

这样可以正常工作,但不需要写list(range(101)),你可以写range(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循环,就像使用challenge 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库,我从未使用过,但是我建议这样做是为了解决这个问题。

我希望这有一些帮助:)

答案 2 :(得分:0)

挑战一:

这是正确的方法。

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。该库将为您提供以图形方式处理数据的工具。

答案 3 :(得分:0)

挑战1的答案如下:

l = []

对于范围(0,100):

    b = 2 ** a

    l.append(b)

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

答案 4 :(得分:0)

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)