我试图为rand range语句添加一个用户输入,并在列表中添加它将要排序的总数,我已尽力解决这个问题,但似乎没有任何工作正常。
import time
import random
def create_random_values():
totalsortnum = (eval("Please enter the amount of numbers you wish to sort: "))
rng1 = (eval("Please enter the maximum number range: "))
randomList=[]
for i in range (totalsortnum):
randomList.append(random.randrange(1,rng1))
return randomList, totalsortnum, rng1
def main():
numList = create_random_values()
print()
print("Here is a random list of 20 numbers between 1 and 1000:")
print(numList)
pause = input("Hit <Enter> to start sorting")
timeInitial = time.time()
l = len(numList)
for lastitem in range(l-1,0,-1):
sorted = True
for i in range(lastitem):
if numList[i] > numList[i+1]:
numList[i],numList[i+1] = numList[i+1],numList[i]
sorted = False
#print(numList)
#pause = input("Hit <Enter> to start sorting")
if sorted == True:
break
timeFinal = time.time()
print(numList)
print("the selection sort of",len(numList),"items took:")
print(timeFinal - timeInitial,"seconds.")
答案 0 :(得分:0)
您的代码存在一些问题:
在create_random_values()
内部,您尝试使用eval()
进行用户输入
eval()
不是您想要的功能,您需要input()
。
main()
numList
内部的(randomList, totalsortnum, rng1)
变量是一个3元组,randomList
,我认为你只想引用import time
import random
def create_random_values():
totalsortnum = int(input("Please enter the amount of numbers you wish to sort: "))
rng1 = int(input("Please enter the maximum number range: "))
randomList=[]
for i in range (totalsortnum):
randomList.append(random.randrange(1,rng1))
return randomList, totalsortnum, rng1
def main():
numList, totalsortnum, rng1 = create_random_values()
print()
print("Here is a random list of {0} numbers between 1 and {1}:".format(totalsortnum, rng1))
print(numList)
pause = input("Hit <Enter> to start sorting")
timeInitial = time.time()
l = len(numList)
for lastitem in range(l-1,0,-1):
sorted = True
for i in range(lastitem):
if numList[i] > numList[i+1]:
numList[i],numList[i+1] = numList[i+1],numList[i]
sorted = False
print(numList)
#pause = input("Hit <Enter> to start sorting")
if sorted == True:
break
timeFinal = time.time()
print(numList)
print("the selection sort of",len(numList),"items took:")
print(timeFinal - timeInitial,"seconds.")
。
这应该适合你:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 12f, jumpHeight = 30f;
Rigidbody2D playerBody;
Transform playerTrans, tagGround;
bool isGrounded = false;
public LayerMask playerMask;
public float maxJumps = 2;
public float jumpsLeft = 2;
// Use this for initialization
void Start ()
{
playerBody = this.GetComponent<Rigidbody2D>();
playerTrans = this.transform;
tagGround = GameObject.Find(this.name + "/tag_Ground").transform;
}
// Update is called once per frame
public void FixedUpdate ()
{
isGrounded = Physics2D.Linecast(playerTrans.position, tagGround.position, playerMask);
Move();
Jump();
DoubleJump();
}
private void Move()
{
float move = Input.GetAxisRaw("Horizontal") * speed;
playerBody.velocity = new Vector2(move, playerBody.velocity.y);
}
private void Jump()
{
if (isGrounded)
{
if (Input.GetButtonDown("Jump"))
{
playerBody.velocity = new Vector2(playerBody.velocity.x, jumpHeight);
}
}
}
private void DoubleJump()
{
if (Input.GetButtonDown("Jump") && jumpsLeft > 0)
{
Jump();
jumpsLeft--;
}
if (isGrounded)
{
jumpsLeft = maxJumps;
}
}
}