我的代码有一些问题,看看main()部分,我输入选择2,它调用open_file.但它需要1作为别的而不是IF ...而当我用1 agin写它只是在屏幕上打印1,我做错了什么?Python版本3.4.2.
import sys
name=input("What is your name? : ")
print ("Welcome " + name)
def main():
print ("What do you want to do")
print ("1) Open A File")
print ("2) open Web Browser")
print ("3) Exit OS")
main_choice=input()
if main_choice == 1:
open_file()
elif main_choice == 2:
web_browser()
elif main_choice == 3:
exit_os()
else:
unknown_number()
def unknown_number():
print ("The choice you made does not exist, please choose a valid option")
main
def open_file():
print ("What do you want …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Run Code Online (Sandbox Code Playgroud)
我刚刚接受了lambda函数,我得到了它们的工作原理,至少,我能理解他们恶魔黑暗魔法背后的邪恶仪式.
但即使Python代码让我疯狂,我仍然不明白为什么这段代码有效?pair没有以任何方式定义为什么存在访问索引,更糟糕的是,为什么该索引很重要(fyi pair[2]超出范围并pair[0]给出正常的有序pairs).
我们到底如何才能获得仅存在于可怕的lambda函数的不纯约束中的虚无?此外,当我们凝视它时,获取虚无的东西如何回归到我们眼中的任何空洞?
我有一个列表字典,summary:
summary = {
'Raonic': [2, 0, 11, 122, 16, 139],
'Halep': [2, 2, 10, 75, 6, 60],
'Kerber': [2, 0, 7, 68, 7, 71],
'Wawrinka': [1, 2, 14, 133, 13, 128],
'Djokovic': [2, 2, 10, 75, 8, 125],
}
Run Code Online (Sandbox Code Playgroud)
我希望打印到屏幕(标准输出)按排名降序排列的摘要,其中排名按照该顺序中的条件1-6(比较项目1(列表的)),如果相等比较项目2 (列表中的),如果它们相等,则比较项目3(列表中的).该比较按降序继续到列表的项目4 .
但是,列表的第5项和第6项的比较必须按升序进行.
输出:
Halep 2 2 10 75 6 60
Djokovic 2 2 10 75 8 125
Raonic 2 0 11 122 16 139
Kerber 2 0 7 68 7 71
Wawrinka …Run Code Online (Sandbox Code Playgroud) 我想检查我的值是否是带有点或逗号的浮点数,但是isdigit()返回带有点的false.我想知道为什么以及如何通过它.
> value = "0.0"
> print value.isdigit():
>>> False
Run Code Online (Sandbox Code Playgroud)
我的代码是:
if "." in value and value.isdigit()
print "ok"
Run Code Online (Sandbox Code Playgroud) 请看以下内容:
def update_page_info(url):
# fetch_page -> parse_page -> store_page
chain = fetch_page.s(url) | parse_page.s() | store_page_info.s(url)
chain()
@app.task()
def fetch_page(url):
return myhttplib.get(url)
@app.task()
def parse_page(page):
return myparser.parse_document(page)
@app.task(ignore_result=True)
def store_page_info(info, url):
PageInfo.objects.create(url=url, info=info)
Run Code Online (Sandbox Code Playgroud) 在速度和代码优化方面有什么更好的选择?执行计算,将结果参考存储在本地,然后像这样返回参考:
def aplusb(a,b):
result = a + b
return result
Run Code Online (Sandbox Code Playgroud)
或将计算直接返回给调用者的结果返回:
def aplusb(a,b):
return a + b
Run Code Online (Sandbox Code Playgroud) 下面提到的程序是开关盒的基本形式。我正在尝试在输出中打印宏变量。有关更多详细信息,请参见输出。它不打印宏变量。
开关盒
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 10;
switch (x)
{
case a: printf("Number is 40");
break;
case d: printf("Number is 50");
break;
default: printf("Default case");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
预期:数字为40
实际:默认情况
请参考第1点和第2点。
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 4; //(Point1)Here I'm declared the x value as 4 which is equal to the above defined one of the macros. Hence the output has printed the expected …Run Code Online (Sandbox Code Playgroud) 所以我知道如何打印整个日期或时间或两者,但我不知道你将如何打印年份.有什么建议 ?
Python 3 BTW
我试图做到这一点,以便每次单击按钮时我都可以运行不同的东西。
Counter = 0
def B_C(Counter):
Counter = Counter + 1
if counter == 1:
print("1")
elif counter == 2:
print("2")
else:
if counter == 3:
Print("3")
Run Code Online (Sandbox Code Playgroud)
但我得到
TypeError: B_C() takes exactly 1 positional argument (0 given)
Run Code Online (Sandbox Code Playgroud) 我有一个列表,其中包含字符串和数字的混合,例如
old_list = [23, 35, string, 42, string]
Run Code Online (Sandbox Code Playgroud)
我希望列表看起来像这样
new_list = [23,35,45]
Run Code Online (Sandbox Code Playgroud)
有一个简单的方法吗?
python ×9
python-3.x ×3
c ×1
date ×1
dictionary ×1
digit ×1
lambda ×1
list ×1
naming ×1
performance ×1
python-2.7 ×1
sorting ×1
typeerror ×1