小编Stu*_*e75的帖子

我在python中使用此代码时出现"无效语法"错误.帮我找一个补救措施?

def(pentagon):块中我命名了一个变量" first".但是,这会导致"无效语法"错误.怎么了?我已经尝试过命名其他东西,从单个字母到低级/大写字母组合,如"preArea".

def display():
    print('This program will tell you the area some shapes')
    print('You can choose between...')
    print('1. rectangle    2. triangle')
    print('3. circle       4. pentagon')
    print('5. rhombus      6. trapezoid')

def shape():
    shape = int(input('What shape do you choose?'))
    if shape == 1: rectangle()
    elif shape == 2: triangle()
    elif shape == 3: circle()
    elif shape == 4: pentagon()
    elif shape == 5: rhombus()
    elif shape == 6: trapezoid()
    else:
        print('ERROR: select 1 2 3 4 5 or …
Run Code Online (Sandbox Code Playgroud)

python

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

调用了错误的函数?

所以我在Python中编写了一个简短而简单的聊天机器人,但这是一个令人恼火的问题.程序posResponses() 最初只会调用该函数.

在上下文中,如果我用"悲伤","可怕"甚至"asdfasdfasdf"回答其最初的问题,我仍会得到积极的回应.

应该发生的是如果我输入一个否定/不明确的关键字,应该调用negResponses()/ ambiguousResponses()function.事实并非如此.我做错了什么,我该如何解决?代码如下:

import random
import time

def opening():
    print('Hello!')

def responseType():
    responseType = str(input('How are you ?'))
    if responseType == 'good' or 'great' or 'fantastic' or 'decent' or 'fine' or 'ok' or 'okay': posResponses()
    elif responseType == 'bad' or 'terrible' or 'sad' or 'grumpy' or 'angry' or 'irritated' or 'tired': negResponses()
    else: ambiguousResponses()

def posResponses():
    number = random.randint(1, 4)
    if number == 1:
        print('That\'s great! So what\'s up?')
        input()
        ambiguousResponses()
    if number == …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

如何改善使用matplotlib制作的Julia套装的不良视觉效果?

如果你运行我在下面包含的代码(python 2.7),你会发现结果图像是黑暗和模糊的,几乎看起来有条纹在它上面运行.我意识到使用分散函数绘制这样的东西可能是滥用功能.

我仔细阅读了文档并迷失在其中,只是希望有人能告诉我如何让我的Julia套装看起来像你在书本和网上看到的漂亮的彩色印版一样漂亮.

import numpy as np
import matplotlib.pyplot as plt

# Plot ranges
r_min, r_max = -2.0, 2.0
c_min, c_max = -2.0, 2.0

# Even intervals for points to compute orbits of
r_range = np.arange(r_min, r_max, (r_max - r_min) / 200.0)
c_range = np.arange(c_min, c_max, (c_max - c_min) / 200.0)

c = complex(-0.624, 0.435)
xs = []
ys = []
colors = []

for comp in c_range:
    for real in r_range:
        z = complex(real, comp)

        escaped = False
        for …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib fractals python-2.7

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

将列表添加到Haskell中的列表列表中?

我在相关文档中找到的所有内容都是++和concat.

我一开始认为做以下事情会给我想要的东西:

  [1, 3, 4] ++ [4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

但正如你所知,只给[1,2,3,4,5,6].

我需要做什么来接受[1,2,3]和[4,5,6]并且[[1,2,3],[4,5,6]]?

haskell list

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

将整数乘以分数和小数时,Python程序出错

我试图制作一个简短的程序来解决着名的德雷克方程.我让它接受整数输入,十进制输入和小数输入.但是,当程序试图将它们相乘时,我收到此错误(在我输入所有必要值之后,错误发生):

Traceback (most recent call last)
  File "C:/Users/Family/Desktop/Programming/Python Files/1/DrakeEquation1.py", line 24, in <module>
    calc() #cal calc to execute it
  File "C:/Users/Family/Desktop/Programming/Python Files/1/DrakeEquation1.py", line 17, in calc
    calc = r*fp*ne*fl*fi*fc*l
TypeError: can't multiply sequence by non-int of type 'str'
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

def intro():
    print('This program will evaluate the Drake equation with your values')

def calc():
    print('What is the average rate of star formation in the galaxy?')
    r = input()
    print('What fraction the stars have planets?')
    fp = input()
    ne = int(input('What is …
Run Code Online (Sandbox Code Playgroud)

python math equation

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

告诉我这个python代码有什么问题吗?

我对编码比较陌生,所以请帮帮我.代码只会运行到第5行.这段代码可能是一个完整的巴贝尔,但请幽默我.

编辑:没有例外,没有任何反应.要求我在1和2之间进行选择后,代码就会停止.

print('This program will tell you the area some shapes')
print('You can choose between...')
print('1. rectangle')
print('or')
print('2. triangle')

def shape():
    shape = int(input('What shape do you choose?'))

    if shape == 1: rectangle
    elif shape == 2: triangle
    else: print('ERROR: select either rectangle or triangle')

def rectangle():
    l = int(input('What is the length?'))
    w = int(input('What is the width?'))
    areaR=l*w
    print('The are is...')
    print(areaR)

def triangle():
    b = int(input('What is the base?'))
    h = int(input('What is the height?'))
    first=b*h …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
168
查看次数

如何在kivy中设置网格布局的位置(x,y坐标)?

我注意到当你在Kivy中使用网格布局制作按钮时,它们是在(0,0)处创建的,并且根据前一个按钮的长度和宽度移动多个空格.但是,我想在屏幕底部中间有一个3x4网格.

到目前为止我有这个:

import kivy
from kivy.uix.gridlayout import GridLayout
from kivy.app import App
from kivy.uix.button import Button

class CalcApp(App):
    def build(self):
        layout = GridLayout(cols=3, row_force_default=True, row_default_height=50)
        layout.add_widget(Button(text='1', size_hint_x=None, width=100))
        layout.add_widget(Button(text='2', size_hint_x=None, width=100))
        layout.add_widget(Button(text='3', size_hint_x=None, width=100))
        layout.add_widget(Button(text='4', size_hint_x=None, width=100))
        layout.add_widget(Button(text='5', size_hint_x=None, width=100))
        layout.add_widget(Button(text='2', size_hint_x=None, width=100))
        layout.add_widget(Button(text='6', size_hint_x=None, width=100))
        layout.add_widget(Button(text='7', size_hint_x=None, width=100))
        layout.add_widget(Button(text='8', size_hint_x=None, width=100))
        layout.add_widget(Button(text='9', size_hint_x=None, width=100))
        layout.add_widget(Button(text='0', size_hint_x=None, width=100))
        layout.add_widget(Button(text='Enter', size_hint_x=None, width=100))
        return layout

CalcApp().run()
Run Code Online (Sandbox Code Playgroud)

那么,我该如何改变立场?

python grid layout button kivy

0
推荐指数
1
解决办法
6165
查看次数

在这个C++代码中调用函数的方式是什么?

我从c ++入门书中得到了这段代码,这本书旨在解释删除操作符.但是,我不明白的是程序如何调用这两个函数以及它们如何交互.

// delete.cpp -- using the delete operator
#include <iostream>
#include <cstring> // or string.h
using namespace std;
char * getname(void); // function prototype

int main()
{
    char * name; // create pointer but no storage

    name = getname(); // assign address of string to name
    cout << name << " at " << (int *) name << "\n";
    delete [] name; // memory freed

    name = getname(); // reuse freed memory
    cout << name << " at " …
Run Code Online (Sandbox Code Playgroud)

c++ function

0
推荐指数
1
解决办法
94
查看次数

如何在Python中打开文件,Web浏览器和URL,而不是在IDLE中.

我知道您可以在Python GUI中打开文件,浏览器和URL.但是,我不知道如何将其应用于程序.例如,以下都不起作用.(以下是我不断增长的聊天机器人程序的片段):

def browser():
    print('OPENING FIREFOX...')
    handle = webbroswer.get() # webbrowser is imported at the top of the file
    handle.open('http://youtube.com')
    handle.open_new_tab('http://google.com') 
Run Code Online (Sandbox Code Playgroud)

def file():
    file = str(input('ENTER THE FILE\'S NAME AND EXTENSION:'))
    action = open(file, 'r')
    actionTwo = action.read()
    print (actionTwo)
Run Code Online (Sandbox Code Playgroud)

就上述顺序而言,这些错误发生在个别运行中:

OPENING FIREFOX...
Traceback (most recent call last):
  File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 202, in <module>
    askForQuestions()
  File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 64, in askForQuestions
    browser()
  File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 38, in browser
    handle = webbroswer.get()
NameError: global name 'webbroswer' is …
Run Code Online (Sandbox Code Playgroud)

python browser file

0
推荐指数
1
解决办法
4557
查看次数